我繼承助手看到這樣的
那繼承的類被使用new
並調用constructor
無 參數subClass.prototype=new F();
哪些可以(在我眼裏)輕鬆突破有點複雜造成的問題構造函數,在繼承過程中,如果構造函數依賴於特定的參數。
所以要解決這個問題,也能夠做你的問題是關於什麼的,我目前使用的是這樣的:
對不起,我誤解了最初的問題和改進我用一點點的幫助支持擴展和還原。
var base = (function baseConstructor() {
var obj = {
create: function instantiation(extend) {
var instance, args = [].slice.call(arguments);
if (this === base) {
throw new SyntaxError("You can't create instances of base");
} else if (!this.hasOwnProperty("initclosure")) {
throw new SyntaxError("Cannot create instances without an constructor");
} else if (this.singleton && this.instances.length !== 0) {
throw new SyntaxError("You can't create more than one Instance of a Singleton Class");
} else {
if (!extend._class || !extend._class.isPrototypeOf(this)) {
instance = Object.create(this.pub);
this.instances.push(instance);
} else {
args = args.slice(1);
extend._class.remove(extend);
instance = this.extend(extend);
}
this.init.apply(instance, args);
instance.onCreate();
return instance;
}
},
extend: function (instance) {
if (!instance._class.isPrototypeOf(this)) {
return;
}
var extended = Object.create(this.pub);
for (var propInst in instance) {
if (instance.hasOwnProperty(propInst)) {
extended[propInst] = instance[propInst];
}
}
instance._class.remove(instance);
this.instances.push(extended);
return extended;
},
reduce: function (instance) {
if (!instance.instanceOf(this)) {
return;
}
var reduced = Object.create(this.pub);
for (var propRed in instance) {
if (instance.hasOwnProperty(propRed)) {
reduced[propRed] = instance[propRed];
}
}
instance._class.remove(instance);
this.instances.push(reduced);
return reduced;
},
remove: function (instance) {
if (instance.instanceOf(this)) {
var removed = this.instances.splice(this.instances.indexOf(instance), 1)[0];
instance.onRemove();
return removed;
}
},
inherit: function inheritation(specsOpt) {
specsOpt = specsOpt || {};
applyDefaults(specsOpt, {
singleton: false,
anonymous: false
});
var sub = Object.create(this);
sub.pub = Object.create(this.pub);
sub.pub.proto = this.pub;
sub.pub._class = sub;
sub.instances = [];
sub.anonymous = specsOpt.anonymous;
sub.sup = this;
if (specsOpt.singleton) {
sub.singleton = specsOpt.singleton;
sub.getSingleton = getSingleton;
protect.call(sub, {
singleton: {
writable: false,
configurable: false,
enumerable: false
},
getSingleton: {
writable: false,
configurable: false
}
});
}
return sub;
},
initclosure: function Base() {},
instances: [],
pub: {
instanceOf: function (obj) {
if (!obj || !obj.pub) {
return this.className;
}
return obj.pub.isPrototypeOf(this);
},
onRemove: function() {},
onCreate: function() {},
"_class": obj
}
};
/* Helper Functions. --- Use function expressions instead of declarations to get JSHint/Lint strict mode violations
*
* TODO: Maybe add an obj.helper Propertie with usefull functions
*/
var applyDefaults = function (target, obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
target[prop] = target[prop] || obj[prop];
}
}
};
var getSingleton = function() { //To get past the strict violation
return this.instances[0];
};
var protect = function (props, desc) { //Maybe change it a little
for (var prop in props) {
if (props.hasOwnProperty) {
Object.defineProperty(this, prop, props[prop] || desc);
}
}
return this;
};
/* End Helpers
*
* Protecting
*/
Object.defineProperty(obj, "init", {
set: function (fn) {
if (typeof fn !== "function") {
throw new Error("Expected typeof init to be 'function'");
} else if (Boolean(fn.name) === this.anonymous) {
try {
throw new Error("Expected the constructor " + (!this.anonymous ? "not " : "") + "to be Anonymous");
} catch (e) {
console.error(e.stack);
}
}
if (!this.hasOwnProperty("initclosure")) {
this.initclosure = fn;
this.pub.constructor = this.init;
this.pub.className = fn.name;
protect.call(this.pub, {
constructor: false,
className: false
}, {
enumerable: false
});
}
},
get: function() {
var that = this;
var init = function init() {
if (that.pub.isPrototypeOf(this)) {
that.initclosure.apply(this, arguments);
} else {
throw new Error("init can't be called directly");
}
};
init.toString = function() {
return that.initclosure.toString();
};
return init;
}
});
obj.toString = function() {
return "[class " + (this.initclosure.name || "Class") + "]";
};
obj.pub.toString = function() {
return "[instance " + (this.className || "Anonymous") + "]";
};
protect.call(obj, {
create: false,
inherit: false,
toString: false,
onRemove: {
enumerable: false
},
onCreate: {
enumerable: false
},
initclosure: {
enumerable: false
}
}, {
writable: false,
configurable: false
});
protect.call(obj.pub, {
instanceOf: false,
toString: false,
"_class": {
enumerable: false
}
}, {
writable: false,
configurable: false,
enumerable: false
});
return obj;
})();
注:這依賴於Object.create
這是ECMAScript中5推出,因此不被舊版本瀏覽器
支持鑑於繼承幫手,讓我們創建一些 「類」
var Car = base.inherit();
Car.pub.getTopSpeed = function() {
return 100;
};
Car.init = function ClassCar(model) {
this.model = model || this.model || "";
};
Car.pub.type = "car";
既然我們有一個可繼承的superClass,讓Truck
繼承自Car
var Truck = Car.inherit();
Truck.pub.getTopSpeed = function() {
return 20;
};
Truck.pub.type = "truck";
Truck.init = function ClassTruck(model, color) {
Truck.sup.init.apply(this, [].slice.call(arguments));
this.color = color;
};
然後讓創造的Car
var myCar = Car.create("Porsche");
console.log(myCar.getTopSpeed(), myCar.className); //100, ClassCar
一個實例現在,如果我的理解是否正確,你想的Car
myCar
擴展現有的實例成爲Truck
一個實例。
如果是這樣,讓我們做到這一點
var myExtendedTruck = Truck.extend(myCar);
console.log(myExtendedTruck.getTopSpeed(), myExtendedTruck.className); //20, ClassTruck
console.log(myExtendedTruck.instanceOf(Truck)); //true
這僅僅設置了擴展的原型鏈copys實例變量到新的卡車實例。所以Car
實例現在是Truck
實例
或者,如果你想使用構造爲好。 create
也適用於傳遞超類的實例。
然後它被擴展並初始化。
var myConstructedExtendedTruck = Truck.create(myCar, myCar.model, "Yellow");
console.log(myConstructedExtendedTruck.getTopSpeed(), myConstructedExtendedTruck.model, myConstructedExtendedTruck.color); //20 , Porsche , Yellow
現在我們有一個擴展Car
實例,也就是現在的Truck
實例,被那個Trucks
構造構成。
現在,如果我有這個權利,你希望能夠回到一個超類實例。
var myReducedCar = Car.reduce(myExtendedTruck);
console.log(myReducedCar.getTopSpeed(), myReducedCar.className); //100, ClassCar
console.log(myReducedCar.instanceOf(Truck)); //false
這裏有一個Example on JSBin撥弄了一下週圍
編輯注:固定的代碼在類instances
陣列
編輯並取消刪除我的回答,我第一次誤解了這個問題。 – C5H8NNaO4 2013-03-26 11:06:35