0

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);
}
4
  • 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 Commented Aug 29, 2016 at 8:29
  • Thanks @JaromandaX, I realize that, but I want to define mycallback() outside of initializations(). I have updated the question to include this important requirement. Sorry to change the game. It was obvious to me, but I realized it was not originally included in the question :P Commented Aug 29, 2016 at 8:33
  • just: libcallback: function(settings) {..}, remove the function call with extra_var Commented Aug 29, 2016 at 8:34
  • your attempt will set libcallback to undefined, because your IIFE is returning undefined - however, you'll have more issues because mycallback will be called immediately rather than as a callback Commented Aug 29, 2016 at 8:38

2 Answers 2

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

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

5 Comments

Looks promising! I will test and accept if it works. Many thanks!
how can i access lib from within mycallback()? If its an anonymous function inline with the object then its just this, but this does not refer to lib within mycallback()
ah I found i can pass this to mycallback() as a third parameter from the anonymous function during the definition like so: libcallback: function(settings) { mycallback(settings, extra_var, this);}
how can i access lib from within mycallback() - I know you found an answer, but I didn't think there was an issue with accessing lib as it was not treated in any way special in the question - I automatically assumed it'd just be "in scope" at that point (within initializations functions as it is in your code)
I know I did originally said lib was defined globally. This was true at the time I wrote the question, but I have since changed that requirement. No worries about not addressing that - I didn't expect you to read my mind haha
0

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.

Comments

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.