Print all hidden object properties

So, I have an empty object a = {} . When I do console.log(a) , console.dir(a) or even

 for(b in a) { console.log(b); } 

I do not see "hidden properties" like __defineGetter__ , hasOwnProperty , etc.

How to print all the properties of an object?

+6
source share
2 answers

What you need is the non - enumerable properties of the object (and, possibly, those that it inherits from its prototype). I do not believe that there is a standard way to get them through JavaScript.

If you use a debugger and validate an object, usually all properties of the object (and not just enumerated ones) are displayed. Now all major browsers have built-in debuggers: Chrome has Dev tools (Ctrl + Shift + I); IE8 and above have "F12 Developer Tools"; IE7 and earlier can be debugged through the free version of VS.Net; the latest versions of Firefox have built-in tools, for older versions you can get the Firebug plugin; Opera has a Dragonfly.

Update . In the comments on the question you said:

I am using Google Chrome 17, and the only property I see with console.log is __proto__ .

Right {} has no properties at all, just a prototype. If you click on the small arrow to the left of __proto__ , it will show you the properties of __proto__ . hasOwnProperty , toString , etc., all the {} properties are obtained from the prototype (which is Object.prototype ), and not the properties of the object itself.

JavaScript uses prototypal inheritance, which means the object is supported by the prototype. If you try to get the value of a property that the object does not have, the JavaScript engine will look at the prototype of the object to see if the prototype has this property; if so, then this value is used. If the prototype does not have this, the engine looks at the prototype prototype; and so on, until it reaches the root of the hierarchy. That's why you hear about objects that have their own properties. and properties that they inherit.

Here is an example:

Here is the constructor function. We put the property in a prototype that will use the JavaScript engine if we use new Foo to create the object.

 function Foo() { } Foo.prototype.bar = 42; 

Create an object using this constructor:

 var f = new Foo(); 

f has no properties at all, and yet:

 console.log(f.bar); // 42 

... because since f does not have a property called "bar", the engine looks at the prototype f , which is the Foo.prototype object.

Now let's f own the "bar" property:

 f.bar = 67; console.log(f.bar); // 67 

Now let's remove the f property "bar":

 delete f.bar; 

What happens if we try to restore f.bar now?

 console.log(f.bar); 

If you said 42 , you will get top marks. Since f no longer has a property called "bar", we return to its prototype.

Please note that this is a live ratio, therefore:

 Foo.prototype.bar = 96; console.log(f.bar); // 96 

In the third edition of ECMAScript (most browsers implement something in accordance with the 3rd edition), the only way to assign a prototype to an object is to use the property of the prototype constructor function, as described above. With the 5th edition, a more direct way was added: Object.create , which can be passed directly to the prototype object:

 var proto = {bar: 42}; var obj = Object.create(proto); console.log(obj.bar); // 42 proto.bar = 67; console.log(obj.bar); // 67 
+13
source

Object.getOwnPropertyNames(obj)

It will also show every non-enumerable property, although it will not follow as a search for a prototype chain like . does.

I do not know a single method that would go up the prototype chain and show non-enumerable values.

Example:

 var o = Object.create({base:0}) Object.defineProperty(o, 'yes', {enumerable: true}) Object.defineProperty(o, 'not', {enumerable: false}) console.log(Object.getOwnPropertyNames(o)) // [ 'yes', 'not' ] console.log(Object.keys(o)) // [ 'not' ] for (var x in o) console.log(x) // yes, base 

So we conclude:

  • Object.keys() does not go up the chain and does not show non-enumerables
  • for in goes up the chain but does not show non-enumerables

Of course, you can manually climb the prototype chain and use Object.getOwnPropertyNames .

For the case of Object __defineGetter__ and hasOwnProperty are the properties of Object.prototype found in new Object Objects by new Object by searching for a prototype chain. So you can get them with:

 console.log(Object.getOwnPropertyNames(Object.prototype)) 

Exit:

 [ 'constructor', 'toString', 'toLocaleString', 'valueOf', 'hasOwnProperty', 'isPrototypeOf', 'propertyIsEnumerable', '__defineGetter__', '__lookupGetter__', '__defineSetter__', '__lookupSetter__' ] 
+4
source

Source: https://habr.com/ru/post/908033/


All Articles