Object.keys() 可以將對象里的所以的鍵取出來放到一個數組中
1、傳入對象, 返回 包含對象可枚舉屬性和方法的數組
var obj = {'a': 'Beijing', 'b': 'Haidian'};
console.log(Object.keys(obj));
//['a', 'b']
2、傳入字符串,返回索引值
var str = "Beijing";
console.log(Object.keys(str));
// ["0", "1", "2", "3", "4", "5", "6"
3、傳入數組,返回索引值
var arr = ["a", "b", "c"];
console.log(Object.keys(arr));
//["0", "1", "2"]
4、構造函數,返回空數組或者屬性名
function Demo(name, age) {
this.name = name;
this.age = age;
}
console.log(Object.keys(Demo));
// []