OOP: dynamic arguments in callback functions?

Discussion in 'Javascript' started by Tyler, May 21, 2006.

  1. Tyler

    Tyler Guest

    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
    Tyler, May 21, 2006
    #1
    1. Advertising

  2. Tyler

    Ian Collins Guest

    Tyler wrote:
    > 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;
    };

    --
    Ian Collins.
    Ian Collins, May 21, 2006
    #2
    1. Advertising

  3. Tyler

    Tyler Guest

    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
    Tyler, May 22, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Gittens
    Replies:
    1
    Views:
    445
    Peter Hansen
    Jul 10, 2005
  2. SeanDavis12
    Replies:
    2
    Views:
    234
    SeanDavis12
    Nov 29, 2006
  3. Lord Eldritch

    Tkinter callback arguments

    Lord Eldritch, Nov 1, 2009, in forum: Python
    Replies:
    31
    Views:
    980
    Alf P. Steinbach
    Nov 5, 2009
  4. Janus
    Replies:
    10
    Views:
    198
    Richard Cornford
    Jan 11, 2007
  5. oldyork90
    Replies:
    10
    Views:
    335
    Jorge
    Sep 27, 2008
Loading...

Share This Page