11

I understand that every function in JavaScript is a first-class object and it has an internal property [[scope]] which hosts the binding records of the function's free variables. However, there are two special cases.

  1. Is the function created by Function constructor also a closure? The function object created by Function constructor is special, because its [[scope]] may not refer to the lexical environments of its outer functions, but only the global context. For example,

    var a = 1; 
    var fn = (function outer() {
        var a = 2; 
        var inner = new Function('alert(a); ');
        return inner;
    })();
    fn(); // will alert 1, not 2.
    

    This is unintuitive. Is this also called closure?

  2. If an inner function doesn't have any free variables, can we say a closure is formed when the inner function is created? For example,

    // This is a useless case only for academic study
    var fn = (function outer() {
        var localVar1 = 1,
            localVar2 = 2;
        return function() {};
    })();
    

    In this case, fn refers to an empty function object which was created as an inner function. It has no free variables. In this case can we say a closure is formed?

leslie.zhang
  • 381
  • 4
  • 14
  • 3
    So what is your definition of "closure"? – Bergi May 15 '15 at 06:09
  • 2
    Further comments on this behavior available here: http://www.bennadel.com/blog/1909-javascript-function-constructor-does-not-create-a-closure.htm The behavior seems related to `eval` behavior (indirect vs. direct call). Also see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function --- *"Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. "* – Mörre May 15 '15 at 06:09
  • 1
    Question #2 is more of a philosophical "if a tree falls in a forest" question. – JJJ May 15 '15 at 06:14
  • @MörreNoseshine Thanks for the links to Ben Nadel's posts and MDN. – leslie.zhang May 15 '15 at 06:27

2 Answers2

11

Is the function created by Function constructor also a closure?

Yes, it closes over the global scope. That might be unintuitive because all other JavaScript closures close over their lexical scope, but it still matches our definition of a closure. In your example, a is a free variable, and resolves to the a in an other scope when the inner/fn function is called somewhere.

If an inner function doesn't have any free variables, can we still call it a closure?

Depends on whom you ask. Some say Yes, others call them "uninteresting closures", personally I say No because they don't reference an outer scope.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Thanks @Bergi, wonderful explanation. In the second case, the outer() function will be garbage collected after we execute the code, since it's no longer referenced. But what's in the [[scope]] internal property of fn? Or put it simply, does the function object fn refers to remember it was created in outer() scope? – leslie.zhang May 15 '15 at 07:10
  • Well, if you follow the ES5 spec the [[scope]] of `inner` stores the lexical environment of `outer`, with its `outer` and `a` variables - so everything is still accessible and remembered. If you follow an actual ES implementation, and inspect `inner` with your debugger, you don't see any scope references - it doesn't need them, as there are no free variables. – Bergi May 15 '15 at 07:15
5

Note: Functions created with the Function constructor do not create closures to their creation contexts; they always are created in the global scope. When running them, they will only be able to access their own local variables and global ones, not the ones from the scope in which the Function constructor was called. This is different from using eval with code for a function expression.

from https://developer.mozilla.org

Mrluobo
  • 85
  • 8