在下面的代码中,当从setTimeout
调用somethingUseful.thisUsefulThing
时,是否可以参考somethingUseful.thatUsefulThing
?JavaScript:功能词典:功能参考可以从它的词典中起作用吗?
var somethingUseful = {
thisUsefulThing: function() {
this.thatUsefulThing();
},
thatUsefulThing: function() {
console.log("I am useful!");
}
}
setTimeout(somethingUseful.thisUsefulThing, 1000);
现在,我得到这个错误:
Uncaught TypeError: Object [object global] has no method 'thatUsefulThing'
你是什么意思“全球范围”vs“范围的对象”?您的代码如同书面作品。 – user2736012
请记住,当您执行'object.method()'时,'method'中的'this'的值将成为对'object'的引用。在创建对象和方法的时候或时间没有区别。重要的是*你怎么称呼这个方法。把同样的方法放在不同的对象上,从该对象调用它,'this'的值将引用新的对象。 'var o = {thatUsefulThing:function(){console.log(“I'm too too!”);}}; o.foo = somethingUseful.thisUsefulThing; o.foo(); //我也很有用' – user2736012
那里,我编辑它直接反映我遇到的错误简而言之。 – Synthead