[ACCEPTED]-How do I pass multiple arguments into a javascript callback function?-callback
Accepted answer
You probably want to use the apply method
this.callback.apply(this, parameters);
The first parameter 2 to apply indicates the value of "this" within 1 the callback and can be set to any value.
Another way now available is to use spread syntax.
this.callback(...callbackFuncParameters)
Here 2 it is again with the full example from the 1 OP:
function doSomething(v1,v2) {
console.log('doing', {v1, v2});
}
function SomeClass(callbackFunction, callbackFuncParameters) {
this.callback = callbackFunction;
this.method = function(){
this.callback(...callbackFuncParameters); // spread!
}
}
var obj = new SomeClass( doSomething, Array('v1text','v2text') );
obj.method()
// output: doing {v1: "v1text", v2: "v2text"}
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.