0
我在Backbone View中有一个函数,它可以从事件中调用。在查看不同的功能得到所谓基于一个名为selected
变种,所以我做了一个查找表来路由在我看来,选择函数值:在Backbone View中使用查找表调用函数没有将“this”(视图)传递给函数
var cases = {
'application' : this.choose_application,
'device' : this.choose_device,
'log' : this.choose_log,
'process' : this.choose_process,
'server' : this.choose_server,
'network' : this.choose_network
}
然后我调用该函数:
var func = cases[selected];
func();
当调用查找表中的函数(例如this.choose_log
)时,choose_log
函数中的this
等于Window对象而不是View。
正如预期的那样,如果不使用查找表,我只需在choose_log
函数中调用this.choose_log()
,this
等于View。
我该如何通过使this
等于我查找表中所有功能的查看?更重要的是,为什么会发生这种情况?