博客 > 前端&移动端&桌面端
What's the difference between `Object.getOwnPropertyNames` and `Object.keys` in javascript? Also some examples would be appreciated. asked Mar 26, 2014 at 10:43 [![kamilkp's user avatar](https://www.gravatar.com/avatar/d801abb584c3211d81e807ae298fe52d?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/2137653/kamilkp) 2 There is a little difference. `Object.getOwnPropertyNames(a)` returns *all* own properties of the object `a`. `Object.keys(a)` returns *all enumerable* own properties. It means that if you define your object properties without making some of them `enumerable: false` these two methods will give you the same result. It's easy to test: ``` var a = {}; Object.defineProperties(a, { one: {enumerable: true, value: 1}, two: {enumerable: false, value: 2}, }); Object.keys(a); // ["one"] Object.getOwnPropertyNames(a); // ["one", "two"] ``` If you define a property without providing property attributes descriptor (meaning you don't use `Object.defineProperties`), for example: ``` a.test = 21; ``` then such property becomes an enumerable automatically and both methods produce the same array. [![Scott Enock's user avatar](https://i.stack.imgur.com/u3OSv.jpg?s=64&g=1)](https://stackoverflow.com/users/9690738/scott-enock) answered Mar 26, 2014 at 10:47 [![dfsq's user avatar](https://www.gravatar.com/avatar/8feb0675d5ebf4c11ed13885347cc970?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/949476/dfsq) [dfsq](https://stackoverflow.com/users/949476/dfsq)dfsq 189k24 gold badges232 silver badges252 bronze badges 6 Another difference is in case of array `Object.getOwnPropertyNames` method will return an extra property that is `length`. ``` var x = ["a", "b", "c", "d"]; Object.keys(x); //[ '0', '1', '2', '3' ] Object.getOwnPropertyNames(x); //[ '0', '1', '2', '3', 'length' ] ``` [![Praveen Soni's user avatar](https://www.gravatar.com/avatar/cf3534b837ac76012fbc37db14c2e68b?s=64&d=identicon&r=PG&f=1)](https://stackoverflow.com/users/6885735/praveen-soni) answered Jul 11, 2017 at 19:17 [![manas's user avatar](https://www.gravatar.com/avatar/63eb4f4574e80f482d614700fc144c79?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1373377/manas) [manas](https://stackoverflow.com/users/1373377/manas)manas 5,6239 gold badges41 silver badges54 bronze badges **Literal notation vs constructor when creating object.** Here is something that got me. ``` 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"; } } ``` So in my case the `foo` function didn't work if I gave it objects of the cat2 type. There are other ways to create objects so there could be other kinks in there as well. answered Aug 28, 2019 at 13:01 [![h3dkandi's user avatar](https://www.gravatar.com/avatar/f0349d36974748bedf9b8a004566a1b8?s=64&d=identicon&r=PG&f=1)](https://stackoverflow.com/users/2870783/h3dkandi) [h3dkandi](https://stackoverflow.com/users/2870783/h3dkandi)h3dkandi 9901 gold badge9 silver badges26 bronze badges 2 As was already explained, `.keys` doesn't return non-enumerable properties. Regarding to examples, one of pitfall cases is an `Error` object: some of its properties are non-enumerable. So while `console.log(Object.keys(new Error('some msg')))` yields `[]`, `console.log(Object.getOwnPropertyNames(new Error('some msg')))` yields `["stack", "message"]` ``` console.log(Object.keys(new Error('some msg'))); console.log(Object.getOwnPropertyNames(new Error('some msg'))); ``` answered Jun 21, 2020 at 11:39 [![Shimon S's user avatar](https://www.gravatar.com/avatar/c10021f6305b0aed80dc07f2dbd0978b?s=64&d=identicon&r=PG)](https://stackoverflow.com/users/1211184/shimon-s) [Shimon S](https://stackoverflow.com/users/1211184/shimon-s)Shimon S 3,7982 gold badges26 silver badges33 bronze badges 0 Another difference is that (at least with nodejs) "getOwnPropertyNames" function does not guarantee keys order, that's why I usually use "keys" function : ``` Object.keys(o).forEach(function(k) { if (!o.propertyIsEnumerable(k)) return; // do something... }); ``` answered Jun 7, 2016 at 15:41 [![Olivier Penhoat's user avatar](https://lh3.googleusercontent.com/-IAalpdoXvBY/AAAAAAAAAAI/AAAAAAAAAAA/C-EKNU2mXvA/photo.jpg?sz=64)](https://stackoverflow.com/users/4122342/olivier-penhoat) 4