JavaScript neutron objects How to access the parent object in detail _javascript tips

Source: Internet
Author: User

In traditional object-oriented programming languages, we provide a special syntax for subclasses to access the parent class, and we often need the extra support of the parent class method in implementing the subclass method. In this case, the subclass usually invokes the same method in the parent class, and eventually completes the work.

JavaScript does not have a special syntax like the one above, but we can create a!

function her () {};
Her.prototype.name = ' Anna ';
her.prototype.toString = function () {
var const = This.constructor;
Return const.uber? This.const.uber.toString () + ', ' + this.name:this.name;
}
function his () {};
var F = function () {};
F.prototype = Her.prototype;
His.prototype = new F ();
His.prototype.constructor = her;
His.uber = Her.prototype;
His.prototype.name = ' Jock ';
function child (width, height) {
this.width = width;
this.height = height;
}
var F = function () {};
F.prototype = His.prototype;
Child.prototype = new F ();
Child.prototype.constructor = child;
Child.uber = His.prototype;
Child.prototype.name = ' Los ';
Child.prototype.getArea = function () {return
this.width * this.height;
}

In the process of building a relationship, we introduced a Uber attribute and made it point to the parent and object.

Here, we have updated the following:

1. Set the Usber property to a reference to the parent object;

2. The ToString () method was updated;

The previous ToString () method simply returned this.name, and now we have added an extra task to check the This.constructor.usber property and call the ToString () method of the property if it exists.

Since This.constructor itself is a function, and This.constructo.usber is a reference to the current object's parent prototype, we call the ToString () method of the child entity, the ToString () on its prototype chain. Method will be invoked.

var i = child (1,2);
My.tostring ()//Anna, Jock, Los

The above is a small set to introduce the JavaScript neutron object access to the parent object of the way, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.