for...in 循环
var obj = { age: 18, fname: "Rand ", lname: "McKinnon" }; function showProps(obj, objName) { var result = ""; for (var prop in obj) { result += objName + "." + prop + " = " + obj[prop] + "\n"; } return result; } console.log(showProps(obj, "person"));
var obj = { age: 18, fname: "Rand ", lname: "McKinnon" }; Object.getOwnPropertyNames(obj).forEach(function (i, value) { console.log(i + ":" + value + "\n") });
Object.keys(obj).forEach(function(i,value){console.log(i+":"+value+"\n")});