__proto__ vs 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.__proto__
is the actual object that is used in the lookup chain to resolve methods, etc. prototype
is the object that is used to build __proto__
when you create an object with new
:
_proto_
var parent = {
color : 'white'
}
var child = {}
child = Object.create(parent)
child.color //White
parent.color = 'black'
child.color // 'black' because of _proto_ chain
Prototype
function Soldier() {}
Soldier.prototype = {
weapons: ['rifle', 'grenade', 'bayonet'],
weaponOfChoice: 'rifle'};
var GIJoe = new Soldier();
var Rambo = new Soldier();
Rambo.weaponOfChoice = 'compound bow';
Rambo.weapons.push(Rambo.weaponOfChoice);
console.log(GIJoe.weaponOfChoice); //because of prototype value not changed
// > rifle
console.log(GIJoe.weapons);
Comments
Post a Comment