var a = {
text : 3,
logText : function() {
console.log(this.text);
},
callLogText : function() {
logText();
}
};
a.callLogText();
这将产生一个ReferenceError: logText is not defined
错误消息。为什么需要“this”来调用对象中的函数
取而代之,你在logText()
方法前加this
,它会好的。没有错误味精会弹出。
var a = {
text : 3,
logText : function() {
console.log(this.text);
},
callLogText : function() {
this.logText();
}
};
我真的不知道原因。
我不认为你向我们展示了所有的代码。我没有在任何地方看到'barral'。 – Raekye 2012-08-02 07:16:17
它与范围有关。 'this'指的是'var a',如果你不使用'this',它会寻找一个叫做'logText'的全局函数。 – Gavin 2012-08-02 07:16:28