1、类式继承
//使subclass继承superclass
function extend(subclass, superclass){
function F() {};
F.prototype = superclass.prototype;
subclass.prototype = new F();//类式继承
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor){
superclass.prototype.constructor = superclass;
}
}
2、原型式继承
function clone(object) {
function F() {};
F.prototype = object.prototype;//原型继承
return new F();
}
3、掺元类
//用givingclass类来扩充receivingclass类
function augment(receivingclass, givingclass) {
if (arguments[2]) {
for (var i = 2, len = arguments.length; i < len; i += 1) {
receivingclass.prototype[arguments[i]] = givingclass.prototype[arguments[i]];
}
} else {
for (methodName in givingclass.prototype) {
if (!receivingclass.prototype[methodName]) {
receivingclass.prototype[methodName] = givingclass.prototype[methodName];
}
}
}
}
function extend(subclass, superclass){
function F() {};
F.prototype = superclass.prototype;
subclass.prototype = new F();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor){
superclass.prototype.constructor = superclass;
}
}function extend(subclass, superclass){
function F() {};
F.prototype = superclass.prototype;
subclass.prototype = new F();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor){
superclass.prototype.constructor = superclass;
}
}
function extend(subclass, superclass){
function F() {};
F.prototype = superclass.prototype;
subclass.prototype = new F();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor){
superclass.prototype.constructor = superclass;
}
}
function extend(subclass, superclass){
function F() {};
F.prototype = superclass.prototype;
subclass.prototype = new F();
subclass.prototype.constructor = subclass;
subclass.superclass = superclass.prototype;
if (superclass.prototype.constructor == Object.prototype.constructor){
superclass.prototype.constructor = superclass;
}
}