Object.keys()返回对象的属性
1 <script> 2 // 传入对象,返回属性名 3 let obj = { 4 'a': '123', 5 'b': '456' 6 } 7 console.log(Object.keys(obj)) //["a","b"] 8 // 传入字符串,返回索引 9 let str = 'abc' 10 console.log(Object.keys(str)) //["0","1","2"] 11 // 构造函数,返回空数组或属性名 12 function Person(name, age, gender) { 13 this.name = name; 14 this.age = age; 15 this.gender = gender; 16 this.toString = function () { 17 return (this.name + ", " + this.age + ", " + this.gender); 18 } 19 } 20 console.log(Object.keys(Person)) // [] 21 // 数组返回索引 22 let arr = ["a", "b", "c"] 23 console.log(Object.keys(arr)) // ["0", "1", "2"] 24 </script>
本文来自博客园,作者:叶子玉,转载请注明原文链接:https://www.cnblogs.com/knuzy/p/9802266.html