Simple and clear use of JavaScript For single inheritance and multi-Inheritance

Source: Internet
Author: User

 

Simple and clear useJavascriptSingle inheritance and multi-Inheritance

By Shen Dongliang http://blog.csdn.net/shendl/

 

Javascript is a powerful multi-generic programming language. It integrates process-oriented, object-oriented, and functional programming with powerful performance capabilities.

Javascript is essentially a functional programming language. It is a 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 JavaScript code, you can implement 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:

FunctionMya (){

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

Mya. Prototype. baseclass. Apply (This, arguments);

}

...... General Code

}

 

FunctionMyb (){

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

Myb. Prototype. baseclass. Apply (this, arguments );

}

...... General Code

}

Myb. Prototype =NewMya ();

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 ();

 

VaRMyb =NewMyb ();

 

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

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

Classname. Prototype. baseclass. Apply (this, arguments );

}

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 =NewBase ();

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(VaRI = 3; I <arguments. length; I ++ ){

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

}

}ElseIf(Arguments. Length = 3 ){

For(VaRNameInGivingclass. 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(VaRI = 3; I <arguments. length; I ++ ){

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

}

}ElseIf(Arguments. Length = 3 ){

For(VaRNameInGivingobject ){

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.

 

*/

 FunctionCopyproperties (target ){

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

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


Copyarrayproperties (target, arguments [I]);

}Else{

For(VaRNameInArguments [I]) {

Target [name]
= Arguments [I] [name];

}

}

}

 

 

}

 

/*

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

*/

FunctionCopyarrayproperties (target, arrayarg ){

For(VaRI = 0; I <arrayarg. length; I ++ ){

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

Copyarrayproperties (target, arrayarg [I]);

}Else{

For(VaRNameInArrayarg [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.