Object.getOwnPropertyNames vs Object.keys
JavaScript中
有一点区别。
很容易测试:
1 2 3 4 5 6 7 | var a = {}; Object.defineProperties(a, { one: {enumerable: true, value: 'one'}, two: {enumerable: false, value: 'two'}, }); Object.keys(a); // ["one"] Object.getOwnPropertyNames(a); // ["one","two"] |
如果在不提供属性属性描述符的情况下定义属性(意味着您不使用
1 | a.test = 21; |
然后,该属性将自动成为可枚举的,并且这两种方法都将产生相同的数组。
另一个区别是数组
1 2 3 | var x = ["a","b","c","d"]; Object.keys(x); //[ '0', '1', '2', '3' ] Object.getOwnPropertyNames(x); //[ '0', '1', '2', '3', 'length' ] |
创建对象时的文字表示法与构造方法。这是让我着迷的东西
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | const cat1 = { eat() {}, sleep() {}, talk() {} }; // here the methods will be part of the Cat Prototype class Cat { eat() {} sleep() {} talk() {} } const cat2 = new Cat() Object.keys(cat1) // ["eat","sleep","talk"] Object.keys(Object.getPrototypeOf(cat2)) // [] Object.getOwnPropertyNames(cat1) // ["eat","sleep","talk"] Object.getOwnPropertyNames(Object.getPrototypeOf(cat2)) // ["eat","sleep","talk"] cat1 // {eat: function, sleep: function, talk: function} cat2 // Cat {} // a partial of a function that is used to do some magic redeclaration of props function foo(Obj) { var propNames = Object.keys(Obj); // I was missing this if // if (propNames.length === 0) { // propNames = Object.getOwnPropertyNames(Obj); // } for (var prop in propNames) { var propName = propNames[prop]; APIObject[propName] ="reasign/redefine or sth"; } } |
因此,在我的情况下,如果我给它赋予cat2类型的对象,则
还有其他创建对象的方法,因此其中也可能存在其他缺陷。
另一个区别是(至少使用nodejs)" getOwnPropertyNames"函数不能保证键顺序,这就是为什么我通常使用" keys"函数:
1 2 3 4 | Object.keys(o).forEach(function(k) { if (!o.propertyIsEnumerable(k)) return; // do something... }); |