I'm getting the error Uncaught TypeError: Function.prototype.apply: Arguments list has wr开发者_运维知识库ong type
when I use the .apply()
method and I'm not sure why. My code is here.
When jsfiddle loads up, click next to the word test and hit the Enter key. The method that the error is occurring in is this.addEvent
. I'm trying to have my object be the 'this' in the event's callback function.
You should use .call
instead of .apply
.
a.apply(obj, lst)
is equivalent to a(lst[0], lst[1], lst[2], ...)
when lst
is an Array (or arguments
) using obj
as this
.
a.call(obj, x, y, z, ...)
is equivalent to a(x, y, z, ...)
using obj
as this
.
Since e
is one of the arguments, not an array of arguments, you should use .call
.
apply
expects an array object. You can also use call
if you want to supply arguments directly. You can also convert arguments to an array using apply
func.apply(editorObject, [e]); //=> apply expects an array of arguments
func.call(editorObject, e); //=> call receives arguments directly
精彩评论