如何在javascript对象文本中引用不同的文本?
如何在javascript对象文本中引用不同的文本?例如:如何在javascript对象文本中引用不同的文本?,javascript,reactjs,Javascript,Reactjs,如何在javascript对象文本中引用不同的文本?例如: var Dispatcher = require('../dispatchers/dispatcher.js'); var App = React.createClass({ handleEvent : function(item){ //... }, events : { dispatcherIndex: Dispatcher.register(function(paylo
var Dispatcher = require('../dispatchers/dispatcher.js');
var App = React.createClass({
handleEvent : function(item){
//...
},
events : {
dispatcherIndex: Dispatcher.register(function(payload) { //register a callback and set the result
this.handleEvent(payload); //this 'this' is the wrong one since this is being called as a callback
//how do i reference the right one (the one that has the handleEvent function)?
})
}
});
如您所见,我们注册了一个回调。因此,该回调中的“this”引用不会引用我们想要的对象。当我们无法为“this”创建一个闭包时,我们该怎么做?您希望
这个
引用到哪里?事件
引用的匿名对象?传递给构造函数的匿名对象<代码>应用程序?@zerkms我希望此
引用包含handleEvent
函数的对象。您想要的东西无法实现。这个events
属性是什么?我不认为它是标准ReactJSAPI的一部分。在构造实例后,您必须调用Dispatcher.register(…)
并存储索引。这意味着您要为每个实例注册一个回调。如果您不想这样做,您必须调整Dispatcher
,以便它允许您触发传递给Dispatcher的回调。使用任意接收器注册(此值)。但是,由于我们不知道调度器是如何工作的,所以我们对此没有什么可说的。