Javascript prototype chain inheritance usage Example Analysis _ javascript skills

Source: Internet
Author: User
This article mainly introduces the use of javascript prototype chain inheritance. The example analyzes the skills and precautions in javascript prototype chain inheritance, which is of great practical value, if you need it, you can refer to the examples in this article to analyze the usage of javascript prototype chain inheritance. Share it with you for your reference. The specific analysis is as follows:

The Code is as follows:

Function Shape (){
This. name = 'shape ';
This. toString = function (){
Return this. name;
}
}

Function TwoDShape (){
This. name = '2d shape ';
}
Function Triangle (side, height ){
This. name = 'trigger ';
This. side = side;
This. height = height;
This. getArea = function (){
Return this. side * this. height/2;
};
}

/* Inheritance */
TwoDShape. prototype = new Shape ();
Triangle. prototype = new TwoDShape ();

When we completely override the prototype attribute of an object, it sometimes has a negative impact on the constructor attribute of the object.
Therefore, it is a good habit to reset the const attributes of these objects after we complete the relevant inheritance relationship settings. As follows:

The Code is as follows:

TwoDShape. prototype. constructor = TwoDShape;
Triangle. prototype. constructor = Triangle;

Rewrite:

The Code is as follows:

Function Shape (){}

Shape. prototype. name = 'shape ';
Shape. prototype. toString = function (){
Return this. name;
}

Function TwoDShape (){}

TwoDShape. prototype = new Shape ();
TwoDShape. prototype. constructor = TwoDShape;

TwoDShape. prototype. name = '2d shape ';

Function Triangle (side, height ){
This. side = side;
This. height = height;
}

Triangle. prototype = new TwoDShape;
Triangle. prototype. constructor = Triangle;

Triangle. prototype. name = 'trigger ';
Triangle. prototype. getArea = function (){
Return this. side * this. height/2;
}

Rewrite (transfer by reference rather than passing by value ):

The Code is as follows:

Function Shape (){}

Shape. prototype. name = 'shape ';
Shape. prototype. toString = function (){
Return this. name;
}

Function TwoDShape (){}

TwoDShape. prototype = Shape. prototype;
TwoDShape. prototype. constructor = TwoDShape;

TwoDShape. prototype. name = '2d shape ';

Function Triangle (side, height ){
This. side = side;
This. height = height;
}

Triangle. prototype = TwoDShape. prototype;
Triangle. prototype. constructor = Triangle;

Triangle. prototype. name = 'trigger ';
Triangle. prototype. getArea = function (){
Return this. side * this. height/2;
}

Although the efficiency is improved, this method has a side effect. Because it is a reference transfer, rather than a value transfer, the name value in the "parent object" is affected.
The sub-object and parent object point to the same object. Therefore, the parent object is changed immediately after the child object is modified.

Then rewrite (use the temporary constructor ):

The Code is as follows:

Function Shape (){}
Shape. prototype. name = 'shape ';
Shape. prototype. toString = function (){
Return this. name;
}
Function TwoDShape (){}
Var F = function (){}
F. prototype = Shape. prototype;
TwoDShape. prototype = new F ();
TwoDShape. prototype. constructor = TwoDShape;
TwoDShape. prototype. name = '2d shape ';
Function Triangle (side, height ){
This. side = side;
This. height = height;
}
F. prototype = TwoDShape. prototype;
Triangle. prototype = new F ();
Triangle. prototype. constructor = Triangle;
Triangle. prototype. name = 'trigger ';
Triangle. prototype. getArea = function (){
Return this. side * this. height/2;
}

Although the efficiency is improved, this method has a side effect. Because it is a reference transfer, rather than a value transfer, the name value in the "parent object" is affected.

The sub-object and parent object point to the same object. Therefore, once the sub-object is aligned with the prototype, the parent object is changed.

I hope this article will help you design javascript programs.

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.