您的位置:首页 > 移动开发 > Objective-C

Object.keys(obj)返回参数obj可被枚举的属性

2016-06-06 10:56 453 查看
Object.keys(obj),返回一个数组,数组里是该obj可被枚举的所有属性。请看示例:

示例一:

function Pasta(grain, width, shape) {

this.grain = grain;

this.width = width;

this.shape = shape;

this.toString = function () {

return (this.grain + ", " + this.width + ", " + this.shape);

}

}

console.log(Object.keys(Pasta)); //console: []

var spaghetti = new Pasta("wheat", 0.2, "circle");

console.log(Object.keys(spaghetti)); //console: ["grain", "width", "shape", "toString"]

示例二:

var arr = ["a", "b", "c"];

console.log(Object.keys(arr)); // console: ["0", "1", "2"]

var obj = { 0 : "a", 1 : "b", 2 : "c"};

console.log(Object.keys(obj)); // console: ["0", "1", "2"]

var an_obj = { 100: "a", 2: "b", 7: "c"};

console.log(Object.keys(an_obj)); // console: ["2", "7", "100"]

var my_obj = Object.create({}, { getFoo : { value : function () { return this.foo } } });

my_obj.foo = 1;

console.log(Object.keys(my_obj)); // console: ["foo"]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: