3

I am preparing for a job interview as a JS developer and I bumped into this closure definition:

A closure is a function defined inside another function (called the parent function), and has access to variables that are declared and defined in the parent function scope.

Let's consider two different pieces of code:

function a() {
    const temp = 1;

    function b() { 
        console.log(temp); 
    }        
}  

So obviously function b is a closure, because it was declared inside function a and has access to its variable temp.

But what if I declare just a function just like that, without an IIFE:

function c() {
    alert("Hi") // a function taken from the global scope
}

My c function wasn't declared inside any function, yet it has access to the global scope. Can it be called a closure, or should it be specifically declared inside another function to be called one?

  • 2
    Maybe there are some freak cases i did not think about, but afaik, technically every function you declare is a closure. You typically only call them that when you want to emphasize it accesses variables declared in its parent scope though. – ASDFGerte Aug 05 '17 at 00:59
  • A closure has three scope chains, it's own defined variables, those of the enclosing function, and the global variables. Your second example only has local and global scope chains so no, it's not a closure. – James Aug 05 '17 at 00:59
  • 1
    I am not versed well enough in the spec to be sure about my answer. However, from what i read [ECMAScript function objects encapsulate parameterized ECMAScript code **closed over** a lexical environment and support the dynamic evaluation of that code.](https://www.ecma-international.org/ecma-262/8.0/index.html#sec-ecmascript-function-objects) [A global environment is a Lexical Environment which does not have an outer environment.](https://www.ecma-international.org/ecma-262/8.0/index.html#sec-lexical-environments) [MDN definition](https://developer.mozilla.org/en/docs/Web/JavaScript/Closures) – ASDFGerte Aug 05 '17 at 02:01
  • 1
    @James—there is only one scope chain. There may be variables from more than one execution context on the scope chain. – RobG Aug 05 '17 at 02:55
  • 1
    That definition of a closure is incorrect, see the [*definition on MDN*](https://developer.mozilla.org/en/docs/Web/JavaScript/Closures) (noting that MDN is not an authority, but it's a very useful resource). Every function creates a closure to the variables in its outer contexts. They are more interesting when they persist beyond the life of the function that created it, but that is not a necessary condition for the creation of a closure. – RobG Aug 05 '17 at 02:58

1 Answers1

5

Can it be called a closure

Yes, though I'd say it creates a closure (to the variables in its outer execution contexts) rather that "is" a closure.

or should it be specifically declared inside another function to be called one

No.

Duplicate of How do JavaScript closures work?

RobG
  • 142,382
  • 31
  • 172
  • 209
  • So clearly the definition I encountered is wrong, right? –  Aug 05 '17 at 20:27
  • 2
    @AdrianWydmanski—yes. Closures are usually introduced in the same context as the article [*Private Members in JavaScript*](http://javascript.crockford.com/private.html) by Douglas Crockford. However, while that aspect of closures is the most interesting (i.e. their ability persist beyond the life of the function that creates them), it's not a necessary condition for their creation. – RobG Aug 05 '17 at 22:31