JS class inheritance Simple example code

Source: Internet
Author: User
Tags inheritance


First, we create a Parenizor class that has the get and set methods of member value, and a ToString method that wraps value in parentheses.

function Parenizor (value) {
This.setvalue (value);
}
Parenizor.method (' SetValue ', function (value) {
This.value = value;
return this;
});
Parenizor.method (' GetValue ', function () {
return this.value;
});
Parenizor.method (' ToString ', function () {
Return ' (' + this.getvalue () + ') ';
});

Implementation code

function Zparenizor (value) {
This.setvalue (value);
}
Zparenizor.inherits (Parenizor);
Zparenizor.method ("e;tostring" E;, function () {
if (This.getvalue ()) {
Return This.uber (' ToString ');
}
return " -0-";
});

Class inheritance is a kind of "is ..." relationship, and parasitic inheritance is a about "the original is ... And now is the relationship of .... Constructors play a large number of roles in the construction of objects. Note that the Uber (instead of the Super keyword) is still valid for the privileged method.
Class extension
The dynamic nature of the web effects allows us to add or replace methods for an existing class. We can call the method at any time. We can extend a class at any time. Inheritance is not the way. So we call this a "class extension" to avoid the extends── of Java, but not the same thing--confusing.

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.