Justin808
Justin808

Reputation: 21512

Uncaught TypeError: Function.prototype.apply: Arguments list has wrong type (?)

I'm getting the error Uncaught TypeError: Function.prototype.apply: Arguments list has wrong 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.

Upvotes: 29

Views: 22984

Answers (2)

KooiInc
KooiInc

Reputation: 122906

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

Upvotes: 13

kennytm
kennytm

Reputation: 523214

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.

Upvotes: 45

Related Questions