Currently, the Chrome DevTools inline console object view (not extended) does not display the difference between native properties and inherited prototype properties.
Now undo what happens in smaller steps.
new foo()
creates a new object, the internal proto
property points to foo.prototype
. This means that this object can access all the properties defined in foo.prototype
. He called the prototype chain .
Now, when you set a property with the same name in the object, it “obscures” the prototype property with the same name, turning the latter inaccessible through regular access to the resource (see @loxxy answer using Object.getPrototypeOf(obj)
for access to shaded prototype property).
After adding a function to an object or its prototype, the console allows you to display an extended representation of the object, which distinguishes its own properties from the properties of the prototype. In the following example, I added a prototype q
method to enable this behavior. Properties inherited from the prototype are displayed inside the proto
object of the internal property:
If you just want to have the number of object instances in the constructor prototype, you can use:
var foo = function() { Object.getPrototypeOf(this).p++; } foo.prototype.p = 0; console.log(new foo());
Or without ES5 dependency:
var foo = function() { foo.prototype.p++; } foo.prototype.p = 0; console.log(new foo());
source share