0

Right now this is my understanding of Javascript Objects... var a = {};

Object.defineProperties(a, {
    one: {enumerable: true, value: 'one'},
    two: {enumerable: false, value: 'two'},
});
a.__proto__ = {three : 'three' };

Object.keys(a); // ["one"]
Object.getOwnPropertyNames(a); // ["one", "two"]
for (var i in a) console.log(i); // one  three

so

Object.keys(a); 

Returns object's own keys that are also enumerable.

Object.getOwnPropertyNames(a); // ["one", "two"]

Returns object's own keys including non enumerable.

for (var i in a) 

Returns object's and it prototype chain, only enumerable keys,

So how do you get non enumerable keys from prototype chain?

Code::

var  o = {a:1};

Object.defineProperty(o,'b',{configurable:true, enumerable :false, value:2,writable: true });

o.__proto__ = {c:3};

Object.defineProperty(o.__proto__,'d',{configurable:true, enumerable :false, value:4,writable: true });

Object.keys(o); // ["a"] -only self nonenum props.
Object.getOwnPropertyNames(o); // ["a", "b"] -only self including enum props.
for (var i in o) console.log(i) // a c  -only enum props including from proto.
Muhammad Umer
  • 17,263
  • 19
  • 97
  • 168

0 Answers0