I know that the latter is non-standard. But Is there a difference between Object.getPrototypeOf
vs __proto__
? I'm investigating how the prototype chain in javascript works, and would like to be clear on this part.
Thanks.
prototype is a property of a Function object. It is the prototype of objects constructed by that function. __proto__ is an internal property of an object, pointing to its prototype.
The __proto__ property of Object. prototype is an accessor property (a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null ) of the object through which it is accessed.
Prototypes is a simple way to share behavior and data between multiple objects access using .prototype. proto is also a way to share behavior and data between multiple objects access using __proto__ All the object constructors (function) have prototype properties. All the objects have proto property.
The object used in Object. create() actually forms the prototype of the new object, whereas in the new Function() from the declared properties/functions do not form the prototype. You cannot create closures with the Object.
From MDN:
Object.getPrototypeOf() is the standard implementation of the old and deprecated object.__proto__ property. However it is a read-only method.
So basically they accomplish the same thing if you are reading the value, except __proto__
is non-standard. __proto__
also potentially lets you set the prototype of an existing object, but generally that's not a good idea, so the standard now would be to use a constructor function or Object.create
to create an object with a specific prototype. That said, the ES6 spec also defines a setPrototypeOf
for setting the prototype of an object as well, though for performance reasons, it's best to avoid that unless explicitly necessary.
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