Javascript备忘录-枚举一个对象的所有属

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"));

Object.getOwnPropertyNames(o)

var obj = { age: 18, fname: "Rand ", lname: "McKinnon" };

Object.getOwnPropertyNames(obj).forEach(function (i, value) { console.log(i + ":" + value + "\n") });

Object.keys(o)

Object.keys(obj).forEach(function(i,value){console.log(i+":"+value+"\n")});

  

你可能感兴趣的:(JavaScript)