I’m writing a jQuery plugin and I’m trying to set up a callback that takes a few parameters. How does jQuery determine which parameters are to be sent to a callback function?
In the plugin:
if (callback) {
callback.call(var1, var2, var3);
}
The written callback:
$("#div").myplugin({callback: myfunction});
myfunction(biz,bar,bim) {
alert("I love " + biz + " and " + bar + " and " + bim);
}
biz is set to var2, bar is set to var3, and bim is undefined.
The first argument of the
callfunction is special:where
thisArgishttps://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call
So, it would work if you passed a value for
thisArg:Or, more simply, don’t use
callat all if you don’t needthisin the callback: