好的,假设我有一个像这样的构造函数:
var Base = function() {};
Base.prototype.shmoo = function() { this.foo="shmoo"; }如何创建独立于Base并相互独立地扩展each的其他构造函数?
换句话说,扩展派生构造函数的功能只影响它的对象,而不影响其他对象,既不影响Base,也不影响另一个派生的对象。
我试过了
Extender = function() {};
Extender.prototype = Base.prototype;
Extender.prototype.moo = function() { this.moo="boo"; };当然,这在所有地方都是有效的。
我应该模拟类的层次结构吗?我试着远离这种模式。
发布于 2013-04-21 03:48:39
这将实现原型继承(这是您想要的):
// The Extender prototype is an instance of Base but not Base's prototype
Extender.prototype = new Base();
// Set Extender() as the actual constructor of an Extender instance
Extender.prototype.constructor = Extender; https://stackoverflow.com/questions/16124375
复制相似问题