Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is obj.constructor.prototype not always equal to obj.__proto__?

function Product(name, price) {
  this.name = name;
  this.price = price;
}

const p1 = new Product('Pen', 20);
const p2 = Object.create(p1);

console.log(p1.constructor.prototype === p1.__proto__);  // true
console.log(p2.constructor.prototype === p2.__proto__);  // false

My understanding was these two are always equal (as in the first console.log statement).

But, while doing some tweaks I found this surprising result (second console.log statement).

Can someone please clear up my understanding about the relationship between prototype and __proto__. Thanks in advance!

like image 455
Gourav Pokharkar Avatar asked Aug 01 '20 14:08

Gourav Pokharkar


People also ask

What is the difference between __ proto __ and prototype?

All the objects have proto property. The prototype gives access to the prototype of function using function. proto gives access to the prototype of the function using the object.

What is object __ proto __?

The __proto__ getter function exposes the value of the internal [[Prototype]] of an object. For objects created using an object literal, this value is Object. prototype . For objects created using array literals, this value is Array. prototype .

What is a prototype How does it differ from a constructor?

Short answer. So what's the difference between constructor and prototype? A short answer is that the constructor is a function that is used to create an object, while the prototype is an object that contains properties and methods that are inherited by objects created from a constructor.

What is the prototype of constructor?

constructor. The constructor property returns a reference to the Object constructor function that created the instance object. Note that the value of this property is a reference to the function itself, not a string containing the function's name. Note: This is a property of JavaScript objects.

What's the difference between a prototype and a constructor?

Well, remember, the prototype's value is the constructor that created the object. After updating the prototype of the LaserModels () with RobotModels (), we also wrote over the constructor, because the constructor's value is the Function () that created the object!

Why OBJ constructor here is a and not C?

Why obj.constructor here is A and not C ? The constructor property is defined in the prototype object, and when you assign it you assign all its members. Any members you want to have different values must be defined, or you will inherit constructor, toString, valueOF, and whatever else theprototype contains.

What are JavaScript Object prototypes?

This article has covered JavaScript object prototypes, including how prototype object chains allow objects to inherit features from one another, the prototype property and how it can be used to add methods to constructors, and other related topics.

What is a link between an object and a prototype?

In JavaScript, a link is made between the object instance and its prototype (its __proto__ property, which is derived from the prototype property on the constructor), and the properties and methods are found by walking up the chain of prototypes.


1 Answers

This only works for instances created using new from constructors that follow the standard prototype pattern. These objects will inherit from the constructors .prototype object, which has a .constructor property pointing back to the constructor. This means when accessing the inherited .constructor, we can find the prototype object that we're inheriting on it.

However, it doesn't work for arbitrary objects that have their own .constructor property (e.g. {constructor: null}) or for objects that don't inherit directly from a constructor's prototype object, such as your p2.

To clarify what's happening in your code without using new:

const Product = Object.create(Function.prototype);
Product.prototype = Object.create(Object.prototype);
Product.prototype.constructor = Product;

const p1 = Object.create(Product.prototype);
p1.name = "Pen";
p1.price = 20;
console.assert(Product.prototype == Object.getPrototypeOf(p1));
console.assert(!p1.hasOwnProperty("constructor") && p1.constructor == Product);
console.assert(p1.constructor.prototype == Product.prototype);
console.assert(p1.constructor.prototype == Object.getPrototypeOf(p1));

const p2 = Object.create(p1);
console.assert(p1 == Object.getPrototypeOf(p2));
console.assert(!p2.hasOwnProperty("constructor") && p2.constructor == p1.constructor);
like image 94
Bergi Avatar answered Nov 06 '22 07:11

Bergi