使用 apply() 添加事件监听器
我正在尝试使用 apply() 方法调用 addEventListener() 。代码是这样的:
function rewrite(old){ return function(){ console.log( 'add something to ' + old.name ); old.apply(this, arguments); } } addEventListener=rewrite(addEventListener);
它不起作用。该代码适用于普通 JavaScript 方法,例如,
function hello_1(){ console.log("hello world 1!"); } hello_1=rewrite(hello_1);
需要帮助!
谢谢!
I'm trying to invoke addEventListener() using apply() method. The code is like:
function rewrite(old){ return function(){ console.log( 'add something to ' + old.name ); old.apply(this, arguments); } } addEventListener=rewrite(addEventListener);
It doesn't work. The code works for normal JavaScript method, for example,
function hello_1(){ console.log("hello world 1!"); } hello_1=rewrite(hello_1);
Need help!
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不幸的是,您不能指望
addEventListener
是一个真正的 Javascript 函数。 (对于其他几个主机提供的函数也是如此,例如window.alert
)。许多浏览器都会做正确的事情(tm)并使其成为真正的Javascript函数,但有些浏览器却没有(我正在看着你,微软)。如果它不是真正的 Javascript 函数,则它不会将apply
和call
函数作为属性。因此,您实际上无法使用主机提供的函数来执行此操作,因为如果您想将任意数量的参数从代理传递到目标,则需要
apply
功能。相反,您必须使用特定的函数来创建知道所涉及的主机函数的签名的包装器,如下所示:当您调用该函数时,传入一个元素,它会返回一个函数,该函数将通过 < 将事件处理程序连接到该元素代码>addEventListener。 (请注意,IE8 之前的 IE 没有
addEventListener
;它使用attachEvent
代替。)不知道这是否适合您的用例(如果不适合) ,有关用例的更多详细信息会很方便)。
您可以像这样使用上面的内容:
请注意,当我们调用它时,我们没有将元素引用传递给
proxy
;它已经内置到函数中,因为该函数是一个闭包。如果您不熟悉它们,请参阅我的博客文章 闭包并不复杂可能会有用。这是一个完整的例子:
关于下面关于
func.apply()
与func()
的问题,我想你可能已经明白了,只是我原来的错误答案混乱的事情。但以防万一:apply
调用函数,执行两项特殊操作:this
在函数调用中的内容。您可能知道,Javascript 中的
this
与 C++、Java 或 C# 等其他语言中的this
有很大不同。 Javascript 中的this
与函数的定义位置无关,它完全由函数的调用方式设置。每次调用函数时,都必须将this
设置为正确的值。 (有关 Javascript 中this
的更多信息此处< /a>.) 有两种方法可以做到这一点:this
设置为调用中的对象。例如,foo.bar()
将this
设置为foo
并调用bar
。apply
或call
属性调用该函数;那些将this
设置为其第一个参数。例如,bar.apply(foo)
或bar.call(foo)
会将this
设置为foo
并调用栏
。apply
和call
之间的唯一区别是它们如何接受传递给目标函数的参数:apply
将它们作为数组(或类似数组的东西):而
call
接受它们作为单独的参数:它们都调用
bar
,将this
设置为foo
,并传入参数 1、2 和 3。You can't count on
addEventListener
being a real Javascript function, unfortunately. (This is true of several other host-provided functions, likewindow.alert
). Many browsers do the Right Thing(tm) and make them true Javascript functions, but some browsers don't (I'm looking at you, Microsoft). And if it's not a real Javascript function, it won't have theapply
andcall
functions on it as properties.Consequently, you can't really do this generically with host-provided functions, because you need the
apply
feature if you want to pass an arbitrary number of arguments from your proxy to your target. Instead, you have to use specific functions for creating the wrappers that know the signature of the host function involved, like this:When you call that, passing in an element, it returns a function that will hook up event handlers to that element via
addEventListener
. (Note that IE prior to IE8 doesn't haveaddEventListener
, though; it usesattachEvent
instead.)Don't know if that suits your use case or not (if not, more detail on the use case would be handy).
You'd use the above like this:
Note that we didn't pass the element reference into
proxy
when we called it; it's already built into the function, because the function is a closure. If you're not familiar with them, my blog post Closures are not complicated may be useful.Here's a complete example:
Regarding your question below about
func.apply()
vs.func()
, I think you probably already understand it, it's just that my original wrong answer confused matters. But just in case:apply
calls function, doing two special things:this
will be within the function call.As you probably know,
this
in Javascript is quite different fromthis
in some other languages like C++, Java, or C#.this
in Javascript has nothing to do with where a function is defined, it's set entirely by how the function is called. You have to setthis
to the correct value each and every time you call a function. (More aboutthis
in Javascript here.) There are two ways to do that:this
to the object within the call. e.g.,foo.bar()
setsthis
tofoo
and callsbar
.apply
orcall
properties; those setthis
to their first argument. E.g.,bar.apply(foo)
orbar.call(foo)
will setthis
tofoo
and callbar
.The only difference between
apply
andcall
is how they accept the arguments to pass to the target function:apply
accepts them as an array (or an array-like thing):whereas
call
accepts them as individual arguments:Those both call
bar
, setingthis
tofoo
, and passing in the arguments 1, 2, and 3.