"Notes" JavaScript prototype chain inheritance instance

Source: Internet
Author: User

function Shape () {this.name = ' Shape '; this.tostring = function () {  return this.name;}} function Twodshape () {this.name = ' 2D shape ';} function Triangle (side,height) {this.name = ' Triangle '; 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 rewrite the prototype property of an object, it can sometimes have a negative effect on the object constructor property. Therefore, it is a good practice to reset the const properties of these objects after we have completed the relevant inheritance relationship settings.
TwoDShape.prototype.constructor = Twodshape; Triangle.prototype.constructor = Triangle;


rewrite:

<span style= "Font-family:microsoft yahei;font-size:14px;color: #333333;" >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 = ' Triangle '; Triangle.prototype.getArea = function () {return THIS.SIDE*THIS.HEIGHT/2;} </span>
Rewrite (reference pass instead of Value pass):
<span style= "Font-size:14px;color: #333333;" >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 = ' Triangle '; Triangle.prototype.getArea = function () {return THIS.SIDE*THIS.HEIGHT/2;} </span>



while efficiency is improved, this approach has a side effect because it is a reference pass, not a value pass, so the name value in the parent object is affected. the child object and the parent object point to the same object. So once the child object is aligned with the prototype, the parent object is changed.

Overwrite again (using temporary constructor):

<span style= "Font-size:14px;color: #333333;" >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 = ' Triangle '; Triangle.prototype.getArea = function () {return THIS.SIDE*THIS.HEIGHT/2;} </span>



while efficiency is improved, this approach has a side effect because it is a reference pass, not a value pass, so the name value in the parent object is affected. the child object and the parent object point to the same object. So once the child object is aligned with the prototype, the parent object is changed.

"Notes" JavaScript prototype chain inheritance instance

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.