Simple Method of Single inheritance and multi-inheritance using JavaScript

Source: Internet
Author: User

Javascript is essentially a function. Programming Language It is the descendant of Lisp. At the same time, it adds an object-oriented programming element, giving up some obscure functional language elements.

The functional programming language can implement object-oriented programming, which is unambiguous. The curry method can simulate classes and objects. However, JavaScript provides another method to implement OOP: Prototype inheritance.

Therefore, the implementation of object-oriented programming in Javascript is different from the general functional programming language.

This article will introduce how to implement Single-inheritance and multi-inheritance in JavaScript.

Use prototype to implement Single inheritance:
Many JavaScript libraries provide functions to achieve single inheritance. But in fact, JavaScript itself provides prototype inheritance methods. Therefore, you do not need to provide specific methods for implementation, simply using JavaScriptCodeYou can achieve single inheritance.

However, the prototype inheritance method provided by JavaScript is prone to mistakes. The following shows how to implement prototype inheritance in a concise and clear manner.

Assume that myb inherits the Mya class, the Code is as follows:

Function Mya (){

If (Mya. Prototype. baseclass! = Undefined ){

Mya. Prototype. baseclass. Call (this );

}

...... General Code

}

Function myb (){

If (myb. Prototype. baseclass! = Undefined ){

Myb. Prototype. baseclass. Call (this );

}

...... General Code

}

Myb. Prototype = new Mya ();

Myb. Prototype. baseclass = myb. Prototype. constructor; // use baseclass to reference the constructors of the // base class.

Myb. Prototype. constructor = myb; // restores the constructor attribute of the subclass to determine the type of the object through this attribute in the future.

VaR Mya = new Mya ();

VaR myb = new myb ();

In the above JavaScript code, I added the following code to every constructor:

If (classname. Prototype. baseclass! = Undefined ){

Classname. Prototype. baseclass. Call (this );

}

This code looks for the baseclass method in the prototype. If so, call it.

In the subsequent implementation of prototype inheritance, I created the baseclass attribute for the subclass. Its value is the constructor of the base class. Therefore, as long as you write code in my way, you can ensure that the base class constructor will be executed when the subclass constructs the instance.

Now, if the Mya class inherits the base class, you only need to add three lines of prototype inheritance code:

Mya. Prototype = new base ();

Mya. Prototype. baseclass = Mya. Prototype. constructor; // use baseclass to reference the constructors of the // base class.

Mya. Prototype. constructor = Mya; // restores the constructor attribute of the subclass to determine the type of the object through this attribute in the future.

Without modifying the code of the Mya class, you can run the Mya class and its subclass myb class instances correctly.

The inheritance implemented by those JavaScript libraries requires modifying base classes such as objects and functions, which may cause conflicts. There are also some methods. Each implementation inheritance must manually modify the constructor of the subclass so that the constructor can correctly call the constructor of the base class.

The method provided above solves these problems well by writing JavaScript code.

Use Mixin to implement multi-Inheritance
Javascript itself does not provide a multi-Inheritance Mechanism, but we can use the Mixin technique to implement multi-inheritance.

The following is the Mixin assistant method I have compiled. You can dynamically add attributes to classes and objects.

1. copyclassprototypeproperties: copy the prototype attributes of the class.
/**

* Copy all attributes of the source class prototype to the target object. The first parameter is a Boolean value, indicating whether to overwrite the attributes of the target class prototype.

*/

Function copyclassprototypeproperties (isoverride, receivingclass, givingclass ){

If (arguments. length> 3 ){

For (VAR I = 3; I <arguments. length; I ++ ){

Receivingclass. Prototype [arguments [I] = givingclass. Prototype [arguments [I];

}

} Else if (arguments. Length = 3 ){

For (VAR name in givingclass. Prototype ){

If (isoverride ){

Receivingclass. Prototype [name] = givingclass. Prototype [name];

} Else {

// Do Not Overwrite

If (! Receivingclass. Prototype [name]) {

Receivingclass. Prototype [name] = givingclass. Prototype [name];

}

}
}

} Else {

Throw "please give 3 arguments at least! ";

}
}

2. Copy the attributes of the object in copyobjectproperties.
/*

* Copy all attributes of the source object to the target object. The first parameter is a Boolean value, indicating whether to overwrite the attributes of the target object.

*/

Function copyobjectproperties (isoverride, receivingobject, givingobject ){

If (arguments. length> 3 ){

For (VAR I = 3; I <arguments. length; I ++ ){

Jsoningobject [arguments [I] = givingobject [arguments [I];

}

} Else if (arguments. Length = 3 ){

For (VAR name in givingobject ){

If (isoverride ){

Jsoningobject [name] = givingobject [name];

} Else {

// Do Not Overwrite

If (! Ingingobject [name]) {

Jsoningobject [name] = givingobject [name];

}

}
}

} Else {

Throw "please give 3 arguments at least! ";
}
}

3. Copy properties to an object
/*

* Copy the attribute to the target object. The attribute can contain any number of attributes and can be an array.

*/

Function copyproperties (target ){

For (VAR I = 1; I <arguments. length; I ++ ){

If (arguments [I]. constructor = array ){

Copyarrayproperties (target, arguments [I]);

} Else {

For (VAR name in arguments [I]) {

Target [name] = arguments [I] [name];

}
}
}
}
/*

* Copying attributes in the array format to the target object should not be called directly.

*/

Function copyarrayproperties (target, arrayarg ){

For (VAR I = 0; I <arrayarg. length; I ++ ){
If (arrayarg [I]. constructor = array ){
Copyarrayproperties (target, arrayarg [I]);

} Else {
For (VAR name in arrayarg [I]) {
Target [name] = arrayarg [I] [name];
}
}
}
}
PS: at present, I am planning to write a javascript library that provides many functions with simple code, including UI components.

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.