JavaScript does not use prototype and new to implement the Inheritance Mechanism, and jsprototype inherits
This method is not original. I summarize it based on my predecessors and come up with a simple and practical JavaScript inheritance method.
Traditional JavaScript inherits the prototype-based prototype chain and requires a lot of new operations. The code is not concise enough and the readability is not very strong. It seems that the prototype chain is vulnerable to contamination.
The Inheritance Method summarized by the author is concise and clear. Although it is not the best method, it is hoped to inspire readers.
Okay, you don't need to talk about it. You can directly read the code and make detailed comments. You can understand it at a glance ~~~
Copy codeThe Code is as follows:
/**
* Created by Yang Yuan on 14-11-11.
* Do not use prototype for inheritance
*
*/
/**
* Javascript Object replication: Only one layer is copied, and only function attributes are copied. This is not common!
* @ Param obj refers to the object to be copied.
* @ Returns Object
*/
Object. prototype. clone = function (){
Var _ s = this,
NewObj = {};
_ S. each (function (key, value ){
If (Object. prototype. toString. call (value) = "[object Function]") {
NewObj [key] = value;
}
});
Return newObj;
};
/**
* Traverse all properties of obj
*
* @ Param callback: callback function. The callback contains two parameters: key attribute name and value attribute value.
*/
Object. prototype. each = function (callback ){
Var key = "",
_ This = this;
For (key in _ this ){
If (Object. prototype. hasOwnProperty. call (_ this, key )){
Callback (key, _ this [key]);
}
}
};
/**
* Create a subclass
* @ Param ext obj, which contains methods to be rewritten or extended.
* @ Returns Object
*/
Object. prototype. extend = function (ext ){
Var child = this. clone ();
Ext. each (function (key, value ){
Child [key] = value;
});
Return child;
};
/**
* Create an object (Instance)
* @ Param arguments can accept any number of parameters as the constructor parameter list
* @ Returns Object
*/
Object. prototype. create = function (){
Var obj = this. clone ();
If (obj. construct ){
Obj. construct. apply (obj, arguments );
}
Return obj;
};
/**
* Useage Example
* Using this inheritance method avoids tedious prototype and new.
* However, the example written by the author only inherits the function of the parent class (which can be understood as a member method ).
* If You Want To inherit more rich content, complete the clone method.
*
*
*/
/**
* Animal (parent class)
* @ Type {construct: construct, eat: eat }}
*/
Var Animal = {
Construct: function (name ){
This. name = name;
},
Eat: function (){
Console. log ("My name is" + this. name + ". I can eat! ");
}
};
/**
* Bird (subclass)
* Birds rewrite the parent class eat method and extend the fly method.
* @ Type {subclass | void}
*/
Var Bird = Animal. extend ({
Eat: function (food ){
Console. log ("My name is" + this. name + ". I can eat" + food + "! ");
},
Fly: function (){
Console. log ("I can fly! ");
}
});
/**
* Create a bird instance
* @ Type {Jim}
*/
Var birdJim = Bird. create ("Jim "),
BirdTom = Bird. create ("Tom ");
BirdJim. eat ("worm"); // My name is Jim. I can eat worm!
BirdJim. fly (); // I can fly!
BirdTom. eat ("rice"); // My name is Tom. I can eat rice!
BirdTom. fly (); // I can fly!