码迷,mamicode.com
首页 > 编程语言 > 详细

Javascript——枚举一个对象的所有属性

时间:2015-05-18 22:39:23      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:

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——枚举一个对象的所有属性

标签:

原文地址:http://www.cnblogs.com/fengchengjushi/p/4513124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!