Implementation of the Inheritance Mechanism Based on JavaScript: How to Use the constructor method object impersonate _ basic knowledge

Source: Internet
Author: User
We know that JavaScript is an object-oriented scripting language, so since it is object-oriented, inheritance must be indispensable. The core of JavaScript is ECMAScript. The implementation of the JavaScript Inheritance Mechanism is actually the implementation of the ECMAScript Inheritance Mechanism. Inheritance Method

ECMAScript implements inheritance in more than one way. This is because the Inheritance Mechanism in JavaScript is not clearly defined, but achieved through imitation. This means that all inheritance details are not completely handled by the interpreter. As a developer, you have the right to decide the most suitable inheritance method. The most primitive inheritance implementation method is object impersonating. The following describes this method.

Object impersonating

The core of object impersonation inheritance actually relies on the use of the this keyword in the function environment. The principle is as follows: the constructor uses the this keyword to assign values to all attributes and methods (that is, the constructor method of class Declaration ). Because the constructor is just a function, you can make the ClassA constructor A ClassB method and then call it. ClassB receives the attributes and methods defined in the ClassA constructor. For example, use the following method to define ClassA and ClassB:

The Code is as follows:


Function ClassA (sColor ){
This. color = sColor;
This. sayColor = function (){
Alert (this. color );
};
}

Function ClassB (sColor ){
}


This keyword references the object Currently created by the constructor. However, in this method, this points to the object to which it belongs. This principle is to use ClassA as a general function to establish an inheritance mechanism, rather than as a constructor. The following uses the constructor ClassB to implement the Inheritance Mechanism:

The Code is as follows:


Function ClassB (sColor ){
This. newMethod = ClassA;
This. newMethod (sColor );
Delete this. newMethod;
}


In this Code, newMethod is assigned to ClassA (remember that the function name is just a pointer to it ). Call this method and pass it the sColor parameter of the ClassB constructor. The last line of code deletes the reference to ClassA, so that it cannot be called later.

All new attributes and methods must be defined after the code line of the new method is deleted. Otherwise, the attributes and methods of the superclass may be overwritten:

The Code is 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 verify that the preceding code is valid, run the following example:

The Code is 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 impersonating can implement multi-Inheritance

Interestingly, object impersonation supports multi-inheritance. For example, if there are two classes ClassX and ClassY and ClassZ want to inherit these two classes, you can use the following code:

The Code is as follows:


Function ClassZ (){
This. newMethod = ClassX;
This. newMethod ();
Delete this. newMethod;

This. newMethod = ClassY;
This. newMethod ();
Delete this. newMethod;
}


ClassY has a high priority if ClassX and ClassY have attributes or methods of the same name. Because it inherits from the following class. In addition to this small problem, it is easy to use object impersonate multiple inheritance mechanisms.

Due to the popularity of this inheritance method, the third edition of ECMAScript adds two methods to the Function object, namely call () and apply (). Later, many derived methods for implementing inheritance were actually implemented 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.