Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Object.getPrototypeOf vs __proto__

Tags:

javascript

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.

like image 825
simonzack Avatar asked Jul 20 '13 05:07

simonzack


People also ask

What are differences in between __ proto __ vs prototype?

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.

What is __ proto __ object?

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.

What is prototype and __ proto __?

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.

What is the difference between object create vs New?

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.


1 Answers

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.

like image 79
loganfsmyth Avatar answered Sep 23 '22 00:09

loganfsmyth