码迷,mamicode.com
首页 > 其他好文 > 详细

Object.keys()

时间:2016-04-08 11:55:33      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

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"]

 

Object.keys()

标签:

原文地址:http://www.cnblogs.com/maqunjing/p/5367170.html

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