"Notes" uber--how child objects access the parent object

Source: Internet
Author: User

Reading notes "JavaScript Object-oriented Programming Guide"

function Shape () {}shape.prototype.name = ' shape '; Shape.prototype.toString = function () {var result = []; if (this.constructor.uber) {Result[result.length] = This.constru Ctor.uber.toString (); } Result[result.length] = THIS.name; Return Result.join (', ');}; function Twodshape () {}var F = function () {}; F.prototype = Shape.prototype; Twodshape.prototype = new F (); TwoDShape.prototype.constructor = Twodshape; Twodshape.uber = Shape.prototype; TwoDShape.prototype.name = ' 2d shape '; function Triangle (side,height) {this.side = side; this.height = height;} var F = function () {}f.prototype = Twodshape.prototype; Triangle.prototype = new F (); Triangle.prototype.constructor = Triangle; Triangle.uber = Twodshape.prototype; Triangle.prototype.name = ' Triangle '; Triangle.prototype.getArea = function () {return THIS.SIDE*THIS.HEIGHT/2;} var my = new Triangle (5, ten); My.tostring ()



-"shape, 2d shape, Triangle"
In the code above: 1, the Uber attribute is set to reference 2 for its parent prototype, and the Tosting () method is updated.
Before that, toString () did nothing but return the contents of this.name, and now adds additional tasks to check for the existence of the This.constructor.uber attribute in the object. If present, the ToString method of the property is called first. This.constructor.uber a reference to the parent prototype of the current object. Thus, when the ToString method of the Triangle entity is called, all ToString on its prototype chain is called.

"Notes" uber--how child objects access the parent object

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.