我希望将“myEvent”传递给最后的“myFunction”,但在对象“Event”中,“myEvent”未定义。
var myEvent = new Event(window,"click",myFunction,myEvent);
Event = function(target,type,func,parameter){
console.log(parameter) //undefined
var eventFunction = function(){
func(parameter)
}
this.delete = function(){
target.removeEventListener(type,eventFunction)
}
target.addEventListener(type,eventFunction);
}
myFunction(parameter){
console.log(parameter); // says undefined but I want the object "myEvent"
}
Sry for bad english
答案 0 :(得分:0)
在构造函数根级别中使用var that = this
,然后在func(that)
中使用eventFunction
。无需显式传递对新创建的对象的引用,它已经存在于构造函数中。
如果您需要明确传递myEvent
引用,请执行:
var wrapper = {myEvent: {}};
wrapper.myEvent = new Event(...,wrapper.myEvent);