Difference between Object.getPrototypeOf vs __proto__

object.getprototypeof 'this' is not an object
remove __proto__ from object
__proto__: array(0)
object.create vs new
setprototypeof vs object create
object assign vs object create

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.

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.

Object.getPrototypeOf() vs .prototype, getPrototypeOf() method returns the prototype (i.e. the value of the internal [[​Prototype]] property) of The definition of 'Object. getPrototypeOf() yet, Opera supports the non-standard __proto__ property since Opera 10.50. You see that I have been referring to an objects prototype as internal prototype property and I have used justAnObj.constructor.prototype and Object.getPrototypeOf(justAnObj) to get access to it. I could have done the same using __proto__ which is a pseudo property of any object that can be used to get access to its prototype.

Initially, __proto__ was a Mozilla-only hack in early versions of Mozilla browsers (Firefox prior to 3.5). It was codified in the ECMAScript 3.1 only in 2008.

One difference is that __proto__ can be changed (a bad design practice though) while getPrototypeOf is a reading function.

var parentObject = {"parentKey" : "parentValue"};
var childObject = {"childKey" : "childValue"};
childObject.__proto__ = parentObject;
console.log(childObject.parentKey); // parentValue
Object.getPrototypeOf(childObject) = {"parentKey" : "newValue"};
// ReferenceError: Invalid left-hand side in assignment

Another difference is how they handle the unlikely case of a name conflict:

var myObject = { __proto__ : "conflicts", getPrototypeOf : function () {return "does not conflict"} };
console.log( myObject.getPrototypeOf() ); // "does not conflict"
// all objects inherit from Object.prototype, not Object, so there is no conflict
console.log( Object.getPrototypeOf(myObject) ) // Object {}
console.log( myObject.__proto__ ); // Object {}, the value "conflicts" is lost

Object.prototype.__proto__, The __proto__ is considered outdated and somewhat deprecated (in browser-​only part of the JavaScript standard). The modern methods getPrototypeOf ( rabbit ) === animal ) ; // true Object . The difference between calls. Current standards provide an equivalent Object.getPrototypeOf(O) method, though de facto standard __proto__ is quicker. You can find instanceof relationships by comparing a function’s prototype to an object’s __proto__ chain, and you can break these relationships by changing prototype .

Proxy treats ≪__proto__≫ as a get:

_ = new Proxy({}, {
  get:z=>console.log('a'),
  getPrototypeOf:z=>console.log('b'),
});
_.__proto__/*a*/

—and ≪getPrototypeOf≫ as a getPrototypeOf:

_ = new Proxy({}, {
  get:z=>console.log('a'),
  getPrototypeOf:z=>console.log('b'),
});
Object.getPrototypeOf(_)/*b*/

Object.getPrototypeOf(), Example 0 : [[prototype]] vs __proto__ vs prototype. speakingjs.com The __​proto__ is an accessor property of the Object.prototype object. https://www.​quora.com/What-is-the-difference-between-__proto__-and-prototype. 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.

Summary:

In javascript The __proto__ property of an object and the Object.getPrototypeOf() method are both ways to access the prototype of an object. They both give us access to a reference of the prototype property.

What is the difference then?

Because __proto__ is simply a property of an object and was put there back in the day to access the prototype of an object. __proto__ is now deprecated and there is a chance that certain JS engines don't support this property anymore. Object.getPrototypeOf() and Object.setPrototypeOf() are the function which now should be used in order to retrieve a prototype.

Example:

function Dog (name) {
  this.name = name;
}

Dog.prototype.bark = function () { console.log('woof'); };

let dog = new Dog('fluffie');

// DON'T:
// Old method using __proto__ deprecated!
console.log(dog.__proto__);

// DO:
// Using the newer getPrototypeOf function
console.log(Object.getPrototypeOf(dog));

// What about climbing up the prototype chain like this?
console.log(dog.__proto__.__proto__);

// We can simply nest the Object.getPrototypeOf() method calls like this:
console.log(Object.getPrototypeOf(Object.getPrototypeOf(dog)))

Prototype methods, objects without __proto__, Distinguishing between prototype , [[prototype]] , and __proto__ in JavaScript (​yes, the first The only difference I could think of was that a was a function ( Function object), while b was an getPrototypeOf(someObject); // true  Similarly, b.__proto__ also points to the same object as a.__proto__: Example 1 : creating object by constructor This is a example from Dmitr y, about creating objects by constructor, it is going to show how an prototype and __proto__ works in inheritance mechanism.

Javascript inheritance behind the scene __proto__, [[prototype]] and , From MDN : Object.getPrototypeOf () is the standard implementation of the old and deprecated object .__ proto__ property. However, this is a read-only method. Later, in the year 2015, Object.setPrototypeOf and Object.getPrototypeOf were added to the standard, to perform the same functionality as __proto__. As __proto__ was de-facto implemented everywhere, it was kind-of deprecated and made its way to the Annex B of the standard, that is: optional for non-browser environments.

‍ ‍ Difference between Object.getPrototypeOf and __proto__ , 26.1.1 JavaScript's operations: all properties vs. own properties. Some operations Object.getPrototypeOf(obj: Object) : Object. The best way to set a prototype is when creating an object – via __proto__ in an object literal or via: A method definition inside a class C creates a method in the object C.prototype . Classes are  __proto__ __proto__ is the actual object that is used in the lookup chain to resolve methods. It is a property that all objects have. This is the property which is used by the JavaScript engine for inheritance. According to ECMA specifications it is supposed to be an internal property, however most vendors allow it to be accessed and

Prototype chains and classes • JavaScript for impatient programmers, So what's the difference between __proto__ and prototype property in Javascript​? The subject of prototype is related to Javascript objects and object internal prototype of the myObj object: Object.getPrototypeOf(myObj). We want to make this open-source project available for people all around the world. Help to translate the content of this tutorial to your language!

Comments
  • Last para is wrong because you are confused. An object's prototype has isPrototypeOf prop. Not getPrototypeOf. Object is a function and is not the prototype of an object. ¶ The first line of your code should write isPrototypeOf instead of getPrototypeOf, in other words your answer in the last para is not answering this question but another.