Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it true that every function in JavaScript is a closure?

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?

like image 411
leslie.zhang Avatar asked May 15 '15 06:05

leslie.zhang


People also ask

Is a closure is always a function?

Questions & Answers › Category: Programming Language › A closure is always a function. A closure is always a function.

Is closure unique to JavaScript?

Closures are a very powerful yet underused feature unique to of JavaScript (and other ECMAScript languages). They essentially provide your code with private variables that other scripts can't access.

What is the difference between closure and function?

Difference between Function and Closure Function is declared using func keyword whereas Closure doesn't have func keyword. Function has always name but Closure doesn't have. Function doesn't have in keyword but closure has in the keyword.

How does closure work in JavaScript?

A Closure is a combination of a function enclosed with references to its surrounding state (the lexical environment). In JavaScript, closures are created every time a function is created at run time. In other words, a closure is just a fancy name for a function that remembers the external things used inside it.


2 Answers

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.

like image 150
Bergi Avatar answered Sep 21 '22 08:09

Bergi


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

like image 32
Mrluobo Avatar answered Sep 18 '22 08:09

Mrluobo