Javascript-inheritance (prototype chaining)

Source: Internet
Author: User

The key is to set the prototype attribute of classb to the instance of classa.

 Function  Classb (color ){  This . Color = Color;  This . Saycolor = Function  () {Console. Log (  This  . Color );}}  Function  Classa (name ){  This . Name = Name; This . Sayname = Function  () {Console. Log (  This  . Name )}}
// This sentence allows a to inherit all the attributes and methods of B. classa. Prototype = New Classb (); VaR OA = New Classa (); Oa. Color = "Red" ; Oa. saycolor ();

 

The relationship is as follows:

Instance a can inherit all the attributes and methods in the classa. Prototype object.

Now classa. Prototype points to the classb instance

Therefore, instance a can have the attributes of all methods of the classb instance.

Classb instances can have all the attributes and methods of classb. prototype,

Therefore, instance a can also have all the attributes and methods of classb. prototype.

 

It is connected like a chain, so it is called a prototype chain.

Originally:

 

After classa. Prototype = new classb ();

 

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.