The ability to understand prototype links is poor in terms of language expression.

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.