OOP: dynamic arguments in callback functions?

T

Tyler

Hi all,

I am using some components from a JS library (scriptalicious), where
callback functions are arguments to the constructors of the components
like so:

someClass x (name)
{
this.name = name;
this.slider = new
Control.Slider(this.name+'slide',this.name+'track',{
onSlide:function(v){foo()},
onChange:function(v){bar(v)}
);
}
Since I would like to dynamicaly create these components at runtime, I
would like the engine (the browser) to evaluate the value of a member
variable (this.name) and to pass this to the callback of the event,
like
.... onChange:function(v){bar(this.name)} ...
Is this possible by some escaping mechanism or so? (and am I making
myself clear enough?)?

Thanks a lot in advance
 
I

Ian Collins

Tyler said:
Hi all,

I am using some components from a JS library (scriptalicious), where
callback functions are arguments to the constructors of the components
like so:

someClass x (name)
{
this.name = name;
this.slider = new
Control.Slider(this.name+'slide',this.name+'track',{
onSlide:function(v){foo()},
onChange:function(v){bar(v)}
);
}
Since I would like to dynamicaly create these components at runtime, I
would like the engine (the browser) to evaluate the value of a member
variable (this.name) and to pass this to the callback of the event,
like
.... onChange:function(v){bar(this.name)} ...
Is this possible by some escaping mechanism or so? (and am I making
myself clear enough?)?
If you bind the information you will to provide to the callcack function
to the element with the callback, you can access it through the event.

Something like

element.info = {name:this.name};
element.onChange = callback;

function callback( event )
{
// x-browser code to get the event.

var info = event.target.info;
};
 
T

Tyler

Hi Ian,

thank you for the very handy tip, this is a nice one!
(I have solved my problem differently in the meantime by sending a
backpointer to the calling class instance to the callback, which was
an inbuild option in the nice scriptalicious slider)
But anyhow, this is an interesting method,
Thank you for your suggestion

Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top