【发布时间】:2012-08-02 07:13:14
【问题描述】:
var a = {
text : 3,
logText : function () {
console.log(this.text);
},
callLogText : function() {
logText();
}
};
a.callLogText();
这将生成ReferenceError: logText is not defined 错误消息。
相反,您将this 前缀到logText() 方法,就可以了。不会弹出任何错误消息。
var a = {
text : 3,
logText : function () {
console.log(this.text);
},
callLogText : function() {
this.logText();
}
};
我实在想不通。
【问题讨论】:
-
我认为您没有向我们展示所有代码。我在任何地方都没有看到
barral。 -
它与范围有关。
this指的是var a,如果不使用this,它会寻找一个名为logText的全局函数。
标签: javascript