I want to store a pointer to a function and then call it later with some arguments, but I’m having trouble with how to pass the arguments to the function.
For example:
var MyObject = (function () {
return {
myMethod: function (a, b) {
return a + b;
}
};
}());
var method = MyObject.myMethod;
var args = [2, 5];
method(args);
So in the last line, which I know doesn’t do what I want method(args), I want to essentially call MyObject.myMethod(2, 5), so my array attempt fails and creating an object also fails. How do I pass in the stored arguments to the stored method?
you can use
applywhich accepts an array of arguments as an argumentand in the method you have, you can receive the arguments via the “hidden” “pseudo-array”
argumentsargument.this has advantage over
callsince you can pre-build the argument array and don’t have to enumerate the arguments in the call’s()