我有以下模式
BASE = function () {
var that = {};
var number = 10;
that.showNumber = function(){
that.alertNumber();
}
that.alertNumber = function () {
alert(number);
};
return that;
};
CHILD = function () {
var that = Object.create(BASE());
var secondNumber = 20;
// Override base function
that.alertNumber = function () {
alert(secondNumber);
};
return that;
};
var ch = CHILD();
ch.showNumber();
您能告诉我如何调整受Douglas CrockFord启发的模块模式以完全覆盖alerNumber
功能?到目前为止,showNumber
函数显示10而不是20。
先谢谢大家
JSFiddle代码为here
答案 0 :(得分:5)
你可以改变
that.showNumber = function(){
that.alertNumber();
}
到
that.showNumber = function(){
this.alertNumber();
}
但我不确定为什么你不会简单地使用the prototype-base inheritance model。