Object.keys
传入对象,返回属性名组成的数组
var obj = {"a":123,"b":456};
console.log(Object.keys(obj))------["a","b"]
var obj1 = {100:"jdsi","a":3998,9:"dedc"};
console.log(Object.keys(obj1))-----["100","a","9"]
var obj2 = Object.create({},{getFoo:{value:function(){
return this.foo;
}}})
obj2.foo =1;
console.log(Object.keys(obj2))-----["foo"]
传入字符串,返回索引组成的数组
var str = "abck123";
console.log(Object.keys(str))-------[0,1,2,3,4,5,6]
传入构造函数,返回空数组或者属性名
function Person(name,age,gender){
this.name = name;
this.age = age;
this.gender = gender
this.toString = function(){
return (this.name+","+this.age+","+this.gender);
}
}
console.log(Object.keys(Person))----[];
var spa = new Person("Tom",20,"male");
console.log(Object.keys(spa))----["name","age","gener","toString"]
传入数组,返回索引组成的数组
var arr = ["a", "b", "c"];
console.log(Object.keys(arr)); ---- ["0","1", "2"]