I love the Object.getOwnPropertyNames method. It seems like such a useful tool for learning about objects from within a JS shell.
What's driving me nuts, though, is that getOwnPropertyNames seems to be missing some (Note: in my tests I am running an ECMA 5 implementation—Google Chrome version 28.0.1500.95).
Here's an example:
> var x= []
undefined
> x.constructor
function Array() { [native code] }
> Object.getOwnPropertyNames(x)
["length"]
Yet clearly, x has lots of properties! (e.g. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/prototype)
> x.push
function push() { [native code] }
> x.pop
function pop() { [native code] }
Can anyone help explain what's going on here? Thanks! :D
Edit: Okay! I see that getOwnPropertyNames only gets the property names of the object at hand. Is there a simple way to get inherited properties? Or perhaps the only way is to traverse through object.constructor.prototype.__proto__?
According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
The __proto__
property is deprecated and should not be used. Object.getPrototypeOf
should be used instead of the __proto__
getter to determine the [[Prototype]] of an object.
> x = [];
[]
> Object.getOwnPropertyNames(Object.getPrototypeOf(x));
["length", "constructor", "toString", "toLocaleString", "join", "pop", "push", "concat",
"reverse", "shift", "unshift", "slice", "splice", "sort", "filter", "forEach", "some",
"every", "map", "indexOf", "lastIndexOf", "reduce", "reduceRight"]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With