1

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 ?

2 Answers 2

1

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:

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: update.bind(this,arg1, arg2)});

or

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: update.curry(arg1,arg2)});

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:

var update = function(element,d){
    alert(element);
}

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, I tried the bind functions. But I get an "undefined" variable using bind or curry. update.bind(this, 'ok') ... update.curry('ok'); ... var update = function(d, el){ console.log(el); // el is undefined }
in your examples, you're only passing one value to the function. curry and bind pre-load from the beginning... I'll edit
Thanks but even if I change the order, "element" becomes "d" and "d" is undefined.
My bad, I was applying curry on the wrong function. Curry works great thanks.
1

I'm not quite sure what you are trying to accomplish, but something like this?

var options = Object.extend({month: date[0],
                                 year: date[1],
                                 day: date[2],
                             oncalchange: function(d){ return update(d, 'somethingelse');}
                             });

1 Comment

"element" is undefined using this. Actually I instantiate a calendar and oncalchange is an event function (Prototype.emptyFunction) called when I change the value in the calendar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.