Javascript inheritance Implementation Method

Source: Internet
Author: User

Below are several common methods:
1. Object impersonating
Principle: constructor uses the this keyword to assign values to all attributes and methods. Because constructor is just a function, you can make the constructor of ClassA A classB method and then call it. in this way, the classB receives the attributes and methods defined in the classA constructor. example:
Copy codeThe Code is as follows:
Function classA (name)
{
This. name = name;
This. showName = function () {alert (this. name );}
}
Function classB (name)
{
This. newMethod = classA;
This. newMethod (name );
}
Obj = new classA ("hero ");
ObjB = new classB ("dby ");
Obj. showName (); // print hero
ObjB. showName (); // print dby indicates that classB inherits the classA method.

Object impersonating can implement multiple inheritance, for example
Copy codeThe Code is as follows:
Function classz (){
This. newMethod = classX;
This. newMethod ();
Delete this. newMethod;
This. newMethod = classY;
This. newMethod ():
Delete this. newMethod;
}

However, if classX and classY have the same attributes or methods, classY has a high priority.
2. call () method
The call method is similar to the classical object impersonating method. Its first parameter is used as the object of this, and other parameters are directly passed to the function itself.
Copy codeThe Code is as follows:
Function sayName (perfix)
{
Alert (perfix + this. name );
}
Obj = new Object ();
Obj. name = "hero ";
SayName. call (obj, "hello ,");
Function classA (name)
{
This. name = name;
This. showName = function () {alert (this. name );};
}
Function classB (name)
{
ClassA. call (this, name );
}
ObjB = new classB ("bing ");
ObjB. showName (); // indicates that classB inherits the showName method of classA.

3. apply () method
The aplly () method has two parameters. One is used as the this object and the other is an array of parameters passed to the function.
Copy codeThe Code is as follows:
Function sayName (perfix)
{
Alert (perfix + this. name );
}
Obj = new Object ();
Obj. name = "hero ";
SayName. aplly (obj, new Array ("hello ,"));

4. prototype chain
Any attributes and methods of the prototype object will be passed to all instances of the corresponding class. The prototype chain uses this method to show inheritance.
Copy codeThe Code is as follows:
Function classA (){}
ClassA. prototype. name = "hero ";
ClassA. prototype. showName = function () {alert (this. name )}
Function classB (){}
ClassB. prototype = new classA ();
Objb = new classB ()
Objb. showName (); // print hero indicates that B inherits the method.

Note that the parameter is not passed to the classA constructor when it is called. This is the standard practice of the prototype chain and ensures that the constructor does not have any parameters.
All attributes and methods of the subclass must appear after the prototype attribute is assigned a value, and the values assigned before it will be deleted. because the prototype attribute of the object is replaced with a new object, the original object added with the new method will be destroyed.

5 blending mode
The constructor attribute is defined using the impersonating method, and the object method is defined using the original method.
Copy codeThe Code is as follows:
Function classA (name)
{
This. name = name;
}
ClassA. prototype. showName = function () {alert (this. name )}
Function classB (name)
{
ClassA. call (this, name );
}
ClassB. prototype = new classA ();
ClassB. prototype. showName1 = function () {alert (this. name + "*****");};
Obj = new classB ("hero ");
Obj. showName ();
Obj. showName1 ();

In the classB constructor, you can call the call method to inherit the name attribute in classA and use the prototype chain to inherit the showName method of classA.

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.