javascript: access function in closure [closed]

爱⌒轻易说出口 提交于 2021-02-18 21:52:51

问题


I have the name of a function in a variable, but everything is within a closure. With this variable I want to call the function, something like this

(function(func) {
      this[func]() ;         // doesn't work

      function bar() {}
      function foo() {}
})('bar') ;

Is something like this possible or should I, for example, add the functions to a variable, like

(function(func) {        
      var ns = {
          bar: function() {},
          foo: function() {}
      };

      ns[func]() ;         // OK
})('bar') ;

回答1:


Variables and functions declared in the current lexical scope cannot be accessed by name using [] syntax - only property keys (per your second example) can be looked up dynamically based on the contents of another variable.

The only way around this is to resort to eval, which is almost never a good idea.

An exception applies for variables and functions declared in the global scope - those are actually properties of window.




回答2:


In my experience, putting a function inside a closure is one way of declaring it "private" - such that nothing outside can access it. Furthermore, it's worth looking at the way code is minified:

Before:

(function() {
    function privateFn(var1, var2) {
        //TODO
    }
    return {
        publicFn: function() { }
    }
})();

After:

(function() {
    function _a(_0, _1) {

    }
    return {
        publicFn: function() { }
    }
})();

Notice how privateFn doesn't really exist anymore? The minifier knows, by definition of the language, that nothing can access that function outside - by name, or otherwise. You seem to want to make one of your functions public.




回答3:


Something like this:

(new function() {
    this.a = function(x) {
        document.getElementById('out').innerHTML += x;
    }
})['a']('boom.');

http://jsfiddle.net/CT4du/1/

A closure keeps everything private. And normally, this in a closure just refers to the window. But, you can "publicize" items in a "closure" without polluting the global space by turning it into an anonymous object first. This allows you to make one and only one method call on the object before it vanishes ...

You can do normal object things, like use private, lexical variables and expose only the methods you want exposed.

(new function() {

    var a = function(x) { document.getElementById('out').innerHTML += x; }
    var b = function() { a(d); }
    var c = function() { /* ... */ }
    var d = "whatever";

    // expose two of them .. 
    this.a = a;
    this.b = b;

})['a']('boom.');


来源:https://stackoverflow.com/questions/16722489/javascript-access-function-in-closure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!