在 webkit 中对本机 JavaScript 方法使用 apply/call
我试图通过 apply 使用下面的代码调用 addEventListener,但我在 webkit 的控制台中收到“TypeError:类型错误”。
addEvent = (function (handler) {
return function (element, event, fn) {
handler.apply(element, [event, fn, false]);
};
}(addEventListener || attachEvent));
我尝试了 apply 和 call 来调用该方法,但没有成功。我是否错过了一些明显的事情,或者尝试做一些由于我还不知道的原因而不允许的事情?
另一篇文章对此进行了一些讨论,但不完全是我如何尝试使用它; 在 WebKit 中使用本机代码函数作为 JavaScript 对象。
I am trying to invoke addEventListener through apply with the code below but I am getting "TypeError: Type error" in webkit's console.
addEvent = (function (handler) {
return function (element, event, fn) {
handler.apply(element, [event, fn, false]);
};
}(addEventListener || attachEvent));
I have tried both apply and call to invoke the method but to no avail. Am I missing something obvious or trying to do something not allowed for some reason I don't know yet?
Another article is talking about this a little bit but not exactly how I am trying to work with it; Using native code functions as JavaScript objects in WebKit.
实现此目的的另一种方法(正确地 - 正如 Crescent Flash 所指出的)如下:
然后可以扩展该方法以减少两个事件模型之间的其他差异。
Another way of accomplishing this (properly - as noted by Crescent Flash) is as follows :
This could then be extended to reduce the other differences between the two event models.