mulllhausen
mulllhausen

Reputation: 4435

Add a variable to a javascript callback

I am using a library where I must define a callback function, and the library will execute this function upon a certain event:

// initialize the callback for the library.
// `lib` is the main variable for the library and is defined globally
function initializations() {
    var extra_var = 'pass me into the callback';
    var libprops = {
        libcallback: function(settings) { do stuff }
    };
    lib.reconfigure(libprops);
}

The library then runs the callback like so (I have no control over this):

var settings = 'xyz';
libprops.libcallback(settings);

So clearly, one of the variables input to my defined callback will be the settings variable. However I also want to pass in a variable of my own:

function mycallback(settings, extra_var) {
    // do stuff involving settings
    // do stuff involving extra_var
}

How can I define libprops.libcallback within initializations() so that extra_var is passed in, but with function mycallback defined elsewhere? Ie like so:

function mycallback(settings, extra_var) {
    // do stuff involving settings
    // do stuff involving extra_var
}

Is this possible? The reason I want to define mycallback() outside of initializations() is that mycallback() is quite large and its messy to have it defined inside initializations().

It seems like a closure would solve the issue, but I'm not quite sure how to construct it. Here is a preliminary attempt:

function initializations() {
    var extra_var = 'pass me into the callback';
    var libprops = {
        libcallback: (function(settings) {
            mycallback(settings, extra_var)
        })(extra_var)
    };
    lib.reconfigure(libprops);
}

Upvotes: 0

Views: 102

Answers (2)

Jaromanda X
Jaromanda X

Reputation: 1

you already have a closure, therefore you don't need to pass in extra_var anywhere, it's available to your anonymous callback function already, as extra_var

function initializations() {
    var extra_var = 'pass me into the callback';
    var libprops = {
        libcallback: function(settings) {
            // do stuff 
            // you can do stuff with extra_var here too
        }
    };
    lib.reconfigure(libprops);
}

as per extra information in comment, just call your myfunction in the anonymous callback, passing in extra_vars as the second parameter (or first if you want, doesn't matter, it's your function)

function initializations() {
    var extra_var = 'pass me into the callback';
    var libprops = {
        libcallback: function(settings) {
            mycallback(settings, extra_var);
        }
    };
    lib.reconfigure(libprops);
}

there's probably another way using .bind - however, no need for such gymnastics in such a simple scenario

Upvotes: 1

Alberto Chiesa
Alberto Chiesa

Reputation: 7350

Yes, a closure will do, but you don't need to call the function "now". You can create a function that references your local extra_var and pass it around:

function initializations() {
    var extra_var = 'pass me into the callback';
    var libprops = {
        libcallback: function(settings) {
            // you can access both settings passed in as a parameter
            // and extra_var, referenced locally.
            var settingsLocal = settings,
              another = extra_var;
        }
    };
    lib.reconfigure(libprops);
}

If extra_var has a fixed value like in the example, you're done. Elsewhere, you need to elaborate on your request.

Upvotes: 0

Related Questions