The ability to understand prototype links is poor in terms of language expression.
Share my understanding of prototype and prototype chain
Prototype object:
Function People (nameValue, ageValue, fondValue)
{
This. name = nameValue;
This. age = ageValue;
This. fond = fondValue;
}
People. prototype. rule = function ()
{
Console. log (this. name + 'said: Everyone needs to contribute, everyone has a dead ')
}
People. prototype =
{
Insteresting: 'roasted Sweet Potato ',
Work: function ()
{
Console. log ('bricklayer ')
}
}
Var p1 = new People ('dump', 25, 'wa ')
Var p2 = new People ('two', 22, 'taobao ')
// The property cannot be found from the object before it can be found from the object's prototype.
Console. log (p1.insteresting)
// The modification of Object Attributes does not affect the content of the prototype.
P1.insteresting = 'Big watermelon ';
Console. log (p1.insteresting) // large watermelon
Console. log (p2.insteresting) // roast sweet potato
People. prototype. insteresting = 'durian'
Console. log (p2.insteresting) // large durian
Bytes ---------------------------------------------------------------------------------------------------------------
Prototype chain:
Function Baby (name, age, birthDay)
{
This. name = name;
This. age = age;
This. birthDay = birthDay;
}
Baby. prototype. eat = function (food)
{
Console. log (this. name + 'taobao' + food)
}
Baby. prototype. sleep = function (time)
{
Console. log (this. name + 'sleep '+ time + 'hour ')
}
Var baby = new Baby ('huluwa ', 1, new Date (, 1 ))
Console. log (baby. name)
Baby. eat ('Milk powder ')
Baby. sleep ('18 ')
Function Child (name, age, birthDay)
{
// Call function is used to change the point of this in the function. It is used to call the method of object B of object.
Baby. call (this, name, age, birthDay)
}
// Child. prototype = Baby. prototype;
// ---- Conclusion: although the above method can be implemented, the incorrect prototype shares the same memory address.
// If the two prototypes are directly equal, the Child prototype and the Baby prototype are the same prototype.
// If you modify the prototype of Child or the prototype of Baby, the prototype of another object will change.
// The following method is correct, so that Child inherits the prototype of the Baby.
Function Temp (){}
Temp. prototype = Baby. prototype;
Var temp = new Temp ();
Child. prototype = temp;
// Or this can be used
// Child. prototype = new Baby ()
//
// Child. prototype. constructor = Child;
Child. prototype. study = function ()
{
Console. log ('Study hard, daily upgrade ')
}
Child. prototype. play = function (game)
{
Console. log (this. name + 'play' + game)
}
Var child = new Child ('lily', 16, new Date (, 1 ))
Console. log (child. name)
Console. log (child. age)
Child. play ('doll capturing ')
Child. study ()
Child. eat ('haagen-Dazs ')
Child. sleep (9)
Thank you.