The constructor method of implementing inheritance mechanism based on JavaScript the use of Object impersonation _ Basics

Source: Internet
Author: User
Tags inheritance

Way of inheriting

ECMAScript implements inheritance in more than one way. This is because the inheritance mechanism in JavaScript is not explicitly defined, but is done by imitation. This means that all the details of the inheritance are not handled entirely by the interpreter. As a developer, you have the right to decide the most appropriate way to inherit. The most primitive way to implement an inheritance is to impersonate an object, which is described below.

Object impersonate

The core of an object posing as an inheritance is actually dependent on using the This keyword in a functional environment. The principle is as follows: The constructor uses the This keyword to assign values to all properties and methods (that is, the constructor method declared by the class). Because the constructor is just a function, you can make the ClassA constructor a ClassB method and call it. CLASSB will receive the properties and methods defined in the ClassA constructor. For example, define ClassA and CLASSB in the following ways:

Copy Code code as follows:

function ClassA (scolor) {
This.color = Scolor;
This.saycolor = function () {
alert (This.color);
};
}

function ClassB (scolor) {
}


Keyword this refers to the object that the constructor is currently creating. In this method, however, this points to the object to which it belongs. The principle is to use ClassA as a regular function to establish an inheritance mechanism, not as a constructor. You can implement the inheritance mechanism using the constructor ClassB as follows:
Copy Code code as follows:

function ClassB (scolor) {
This.newmethod = ClassA;
This.newmethod (Scolor);
Delete This.newmethod;
}

In this code, a method Newmethod is given to ClassA (remember that the function name is just a pointer to it). The method is then called and passed to it is the parameter scolor of the ClassB constructor. The last line of code deletes a reference to ClassA so that it cannot be invoked later.

All new properties and new methods must be defined after the line of code for the new method has been deleted. Otherwise, the related properties and methods of the superclass may be overwritten:

Copy Code code as follows:

function ClassB (Scolor, sname) {
This.newmethod = ClassA;
This.newmethod (Scolor);
Delete This.newmethod;

THIS.name = sname;
This.sayname = function () {
alert (this.name);
};
}


to prove that the preceding code is valid, you can run the following example:
Copy Code code as follows:

var obja = new ClassA ("Blue");
var objb = new ClassB ("Red", "John");
Obja.saycolor (); Output "Blue"
Objb.saycolor (); Output "Red"
Objb.sayname (); Output "John"

Object Impersonation can achieve multiple inheritance

Interestingly, object impersonation can support multiple inheritance. For example, if there are two classes CLASSX and Classy,classz want to inherit these two classes, you can use the following code:
Copy Code code as follows:

function Classz () {
This.newmethod = CLASSX;
This.newmethod ();
Delete This.newmethod;

This.newmethod = classy;
This.newmethod ();
Delete This.newmethod;
}


There is a disadvantage, if there are two classes CLASSX and classy with the same name of a property or method, classy has a high priority. Because it inherits from the following class. In addition to this small problem, it is easy to use object impersonation to implement multiple inheritance mechanism.

Due to the popularity of this inheritance method, the third edition of ECMAScript added two methods to the Function object, call () and apply (). Later, many of the derived methods of implementing inheritance are actually based on call () and apply ().

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.