1

I looked at this:

Calling a JavaScript function named in a variable

But it doesn't answer my question.

This normally works:

window['class']['sub_class']['function_name'](data);

But now I'm trying to make a general function that can handle any depth:

function callbackFunction(callback, data){
    //callback = a.b.c, or a.b, or a
    callback = explode(callback);      

    //I need to be able to call callbackFunction and somehow callback and form their proper form below
    window[callback.a](data);
    //or
    window[callback.a][callback.b](data);
    //or
    window[callback.a][callback.b][callback.c](data);
}
Community
  • 1
  • 1
lewicki
  • 480
  • 8
  • 21
  • Do you mean `callback` is a string? – bfavaretto Aug 17 '13 at 20:13
  • Yeah, callback is a string. Exploding it to find some solution is possible though. – lewicki Aug 17 '13 at 20:17
  • Sure, but why not just passing an actual function? It can be pre-bound to the correct object with `bind`. – bfavaretto Aug 17 '13 at 20:19
  • I am storing the callback (e.g. "a.b.c") as a string in a button's attribute: – lewicki Aug 17 '13 at 20:20
  • possible duplicate of [Convert string in dot notation to get the object reference](http://stackoverflow.com/questions/10934664/convert-string-in-dot-notation-to-get-the-object-reference) (and many others) – Bergi Aug 17 '13 at 20:21
  • @lewicki: Why don't you just do ``? – Bergi Aug 17 '13 at 20:23
  • @Bergi: because it is part of a form. the callback function will parse all inputs and send it through with the callback – lewicki Aug 17 '13 at 20:24
  • @Bergi Here the target is a function. If you don't fix the `this` binding, you might get unexpected results. So I believe this is half-duplicate. – bfavaretto Aug 17 '13 at 20:35

2 Answers2

1

I believe the duplicate suggested by Bergi will only solve half of your problem. Since your final value will be a function, and since that function is a member of an object, you'll end up executing it in the wrong context (i.e., with the wrong this value).

I suggest you use something like this:

function getCallback(path) {
    var arr = path.split('.');
    var k;
    var fn = window;
    while(k = arr.shift()) {
        if(typeof fn[k] === "function") {
            fn = fn[k].bind(fn);
        } else {
            fn = fn[k]; 
        }
    }

    if(typeof fn === "function") return fn;
    return function(){};
}

http://jsfiddle.net/7CEd5/

Compare the value of this in the callback with what you get by using the answers to Convert string in dot notation to get the object reference.

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

You can chain references to objects/sub-objects/etc for however long you want. If you have a point-delimited string (e.g. "document.blah.blah2.method"), then you need to split it to individual tokens (e.g. ["document", "blah", "blah2", "method"]).

Then it's simply a matter of looping through the chain:

var c = window;
for (var i = 0; i < chain.length - 1; i++) {
  c = c[chain[i]];
}
c[chain[chain.length-1]](some_arguments);
just_dont
  • 141
  • 7