0

In PHP, I can bind a variable to a lambda like this:

$locale = "nl_NL";
$info['t'] = function($text, $render) use ($locale) {
   return translate($text, $locale);
}

In the below code, the fullpath variable gets overwritten and contains the value of the latest call at some point.

var tree = {
    "foo.js": null,
    "bar.js": {
         "baz.js": null
    }
}

loadScriptTree(tree) {
    for (let idx in tree) {
        var fullpath = "root/" + idx;
        console.log("Loading "+fullpath);

        var subtree = tree[idx];            
        $.getScript(fullpath, function() {
            console.log(fullpath+ "loaded.");
            loadScriptTree(subtree);
        });
    }
}

How can I bind the variable to the function in JavaScript?

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195

0 Answers0