[英]How can I pass a local variable from function to event listener function in JavaScript?
Good day! 美好的一天!
I began writing my own basic JavaScript library for personal use and distribution a few days ago, but I am having trouble with one of the methods, specifically bind() . 几天前,我开始编写自己的基本JavaScript库供个人使用和分发,但是我对其中一种方法(特别是bind())感到困惑。
Within the method itself, this refers to the library, the object. 在方法本身内, 这是指库即对象。
I went to Google and found function.call() , but it didn't work out the way I planned it--it just executed the function. 我去了Google并找到了function.call() ,但是它并没有按照我的计划进行,它只是执行了函数。
If you take a look at another method, each() , you'll see that it uses call() to pass values. 如果您看一下另一个方法each() ,您会发现它使用call()传递值。
I also tried the following: 我还尝试了以下方法:
f.arguments[0]=this;
My console throws an error, saying it cannot read '0' of "undefined". 我的控制台抛出一个错误,说它无法读取“未定义”的“ 0”。
I would like to be able to pass this (referencing the library--NOT THE WINDOW) to use it in the event listener. 我希望能够通过此事件 (引用库-NOT THE WINDOW)在事件侦听器中使用它。
You can see it starting at line 195 of the JavaScript of this JSFiddle . 您可以从此JSFiddle的JavaScript的第195行开始看到它。
Here it is as well: 这里也是:
bind:function(e,f){
if(e.indexOf("on")==0){
e=e.replace("on","");
}
if(typeof f==='function'){
/*Right now, 'this' refers to the library
How can I pass the library to the upcoming eventListener?
*/
//f=f(this); doesn't work
//f.call(this); //doesn't work
//this.target refers to the HTMLElement Object itself, which we are adding the eventListener to
//the outcome I'm looking for is something like this:
/*$('h3').which(0).bind(function({
this.css("color:red");
});*/
//(which() defines which H3 element we're dealing with
//bind is to add an event listener
this.target.addEventListener(e,f,false)
}
return this;
},
Thank you so much for your help, contributors! 非常感谢您的帮助,贡献者!
So in this particular case you actually want to call JavaScript's built in bind method that all functions have. 因此,在这种特殊情况下,您实际上想调用所有函数都具有的JavaScript内置绑定方法 。
f = f.bind(this);
f
will be a new function with it's this
argument set to whatever you passed into it. f
将是一个新函数,并且this
参数设置为您传递给它的任何参数。
If, as per your comments, you don't want to use .bind()
, rather than directly passing f
to addEventListener()
you could pass another function that in turn calls f
with .call()
or .apply()
: 如果按你的意见,你不希望使用
.bind()
而不是直接传递f
到addEventListener()
你可以通过另一个函数,进而调用f
与.call()
或.apply()
if(typeof f==='function'){
var _this = this;
this.target.addEventListener(e,function(event){
f.call(_this, event);
},false)
}
Doing it this way also lets your library do any extra event admin, eg, pre-processing on the event
object to normalise properties that are different for different browsers. 这样,您的库还可以让您执行任何额外的事件管理,例如,对
event
对象进行预处理以规范化针对不同浏览器而不同的属性。
Replace f=f(this);
替换
f=f(this);
with f.apply(this);
与
f.apply(this);
Look at underscore code, here: https://github.com/jashkenas/underscore/blob/master/underscore.js#L596 在这里查看下划线代码: https : //github.com/jashkenas/underscore/blob/master/underscore.js#L596
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.