简体   繁体   中英

Console.log shows hidden object info

When I do console.log(someobject) , I see some __proto__ object things inside my object, which contain a huge amount of data in them.

If I have a lot properties on my objects (properties that are also objects), I can easily get hundreds of protos . Anyway does this affect performance in any way? Should I use arrays instead?

It's just part of JavaScript's internal prototype chain. Whenever a new object is created, its __proto__ property is set to its "parent" object's prototype property. To answer your question, it has no impact on performance that you can control directly, so don't worry about it.

If you want to read more about it, check out MDN, https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/Proto

Familiarize yourself with the prototypical inheritance JavaScript uses. See, eg, here at MDN .

In a nutshell: Objects in JavaScript are not created by instantiating classes, but creating an object that is like another object (the prototype). So every object has a pointer to its prototype.

If a method or attribute of an object is required at some point in the code, the compiler checks, whether the object posseses such a property itself (comp. hasOwnProperty() ). If not, it takes a look at the respective prototype object. If the property can not be found there it looks at the prototype of the prototype an so forth. This is done all the way up to Object , which is the base prototype in JavaScript. The chain of prototypes is also called prototype chain .

As this is an inherent feature of the language you can not circumvent it anyways and thus it wont have any impact on your specific site's performance.

This may slow down the console.log calls, but not not your application in production mode. Anyway the __proto__ property of Object objects is a non-standard and deprecated Mozilla extension, it is going to be removed someday so don't worry about it (the standard Object.getPrototypeOf(obj) method can already bu used instead of obj.__proto__ ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM