Reputation: 2926
Why different objects have the same proto with the same assigned properties?
For example:
let a1 = {name: 'firstName'};
let a2 = {name: 'middleName'};
let a3 = Object.assign({}, {name: 'lastName'});
a1.__proto__ === a2.__proto__ // true
a1.__proto__ === a3.__proto__ // true
But if I use constructor function:
let SomeFunc = function(){}
let b1 = new SomeFunc();
b1.__proto__ === a1.__proto__ // false
The __proto__ is different
It looks that all projects created with direct project syntax have the same proto.
Upvotes: 0
Views: 45
Reputation: 664569
Because all the three objects inherit from the same prototype object, Object.prototype
. That's the default for objects created from object literals.
In your second example, b1
does inherit from SomeFunc.prototype
, because it was created with the new
operator. It would be the same as that of const b2 = new SomeFunct
: Object.getPrototypeOf(b2) === Object.getPrototypeOf(b1)
.
Upvotes: 1