Using prototype.js, I create a custom object which is going to be a parameter on a constructor :
var options = Object.extend({month: date[0],
year: date[1],
day: date[2],
oncalchange: update});
// update is defined like that :
var update = function(d){
// bla bla
}
// Calling my class
var myObject = new myClass (options);
On the oncalchange option, I have a callback function called “update”, which takes a parameter within myClass.
Now, I want to pass an extra parameter to the “update” function, but not directly in the class.
I would like to do something like that :
// element is the parameter I want to add
var update = function(d, element){
alert(element);
}
Obviously it doesn’t work, how could I do something similar ?
in prototype, you can use the bind function (belonging to all functions) to pre-fill in values, and assign context. You can use the curry function instead if you do not need to use the context parameter
for example:
or
See http://www.prototypejs.org/api/function/bind for more info on bind, and
http://www.prototypejs.org/api/function/curry for info on curryad
Edit
change your update function to:
then pre-define the element using curry or bind as above.
I assume the callback is expecting a function with one parameter and you want it to take a second which you can define when setting the callback. In such case, the order of the parameters is very important. curry and bind can be used to pre-load arguments from the beginning, and as such, the parameters you want to pre-load should be defined before any of the other parameters.. Hopefully that clears things up.