This method is not original, the author only on the basis of predecessors, summed up a concise and practical JavaScript inheritance method.
Traditional JavaScript inheritance is based on the prototype prototype chain, and requires a lot of new operations, the code is not concise, readability is not very strong, seemingly also vulnerable to the prototype chain pollution.
The author summarizes the way of inheritance, concise and clear, although not the best way, but hope to bring inspiration to the reader.
Well, nonsense not to say, directly look at the code, comments detailed, a look at the Understanding ~ ~ ~
Copy Code code as follows:
/**
* Created by Yang Yuan on 14-11-11.
* Implement inheritance without using prototype
*
*/
/**
* JavaScript Object replication, only one layer copy, and only function properties, not universal!
* @param obj objects 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 obj all its own properties
*
* @param callback callback function. Callback will contain two parameters: Key property name, Value property
*/
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 subclasses
* @param ext obj, containing methods that need to be overridden or extended.
* @returns Object
*/
Object.prototype.extend = function (EXT) {
var child = This.clone ();
Ext.each (function (key, value) {
Child[key] = value;
});
return to child;
};
/**
* Create object (instance)
* @param arguments can accept any number of parameters as a list of constructor parameters
* @returns Object
*/
Object.prototype.create = function () {
var obj = This.clone ();
if (obj.construct) {
Obj.construct.apply (obj, arguments);
}
return obj;
};
/**
* Useage Example
* Use this approach to inherit, avoid cumbersome prototype and new.
* But the example that I'm writing now can only inherit the function of the parent class (which can be understood as a member method).
* If you want to inherit richer content, refine your clone method.
*
*
*/
/**
* Animals (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! ");
}
};
/**
* Birds (subclasses)
* 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!