【发布时间】:2016-11-26 17:36:38
【问题描述】:
我研究 javascript 中的函数继承。 根据我阅读的文章,我已经编写了代码:
function Base(){
var enableVar = true
this.enable = function(){
console.log("enable");
enableVar = true;
}
this.disable = function(){
console.log("disable");
enableVar = true;
}
}
function Child(){
Base.call(this);
}
new Child().enable();
此代码运行正常,我在控制台中看到消息。
但我不明白行:
Base.call(this);
对我来说,它是 Base 函数调用 this 替换为 this 因此它与 Base(); 相同
但是看起来我的状态是错误的。我看到错误:
VM898:62Uncaught TypeError: (intermediate value).enable is not a function(…)
请为我澄清差异。
更新
function funcB(){
return this.a;
}
function funcA(){
this.a = 2;
return funcB();
}
alert(funcA());
虽然我像 funcB(); 一样调用 funcB,但此代码会发出警报 2
我真的不明白区别
【问题讨论】:
标签: javascript inheritance this