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>

 

posted @ 2018-10-17 09:54  叶子玉  阅读(1217)  评论(0编辑  收藏  举报