0

Is the code below a closure? Why?

var getContact = (function(){
 var person = {name: "John Doe"};
 return {aFriend: person};
})();
console.log(getContact.aFriend.name);
//outputs: John Doe
Ala Eddine JEBALI
  • 7,033
  • 6
  • 46
  • 65
  • 1
    NO... This is not a closure. You are just copying that variable to an object's property's value. – Rajaprabhu Aravindasamy Jan 07 '17 at 11:47
  • It **has** a closure. The code below can't be a closure, the code below might have a variable as a closure, but the code itself isn't the closure. Problem of grammar. – SOFe Jan 07 '17 at 12:07
  • 1
    [is-it-true-that-every-function-in-javascript-is-a-closure](http://stackoverflow.com/questions/30252621/is-it-true-that-every-function-in-javascript-is-a-closure) – Legends Jan 07 '17 at 12:14

3 Answers3

5

No.

There is no function declared inside another function that is accessible after the outer function has finished executing.

In this example:

function createClosure() {
    var foo = 0;
    function bar() {
        alert(foo);
    }
    return bar;
}

var myFunc = createClosure();

… the variable foo is closed over so there is a closure.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Is this a closure?: `var getContact = (function(){ var person = {name: "John Doe"}; var message= "Hi message"; function hi(){alert(message);} return {aFriend: person, sayHi:hi}; })(); console.log(getContact.sayHi());` – Legends Jan 07 '17 at 11:56
  • 1
    @Legends: The `hi` function is a closure that persists as long as `getContact` keeps a reference to the object and the object keeps a reference to it via the `sayHi` property, yes. – T.J. Crowder Jan 07 '17 at 11:57
2

All JavaScript functions are closures; they keep a reference to the lexical environment object that's active when they're created. So technically there is briefly a closure created there, but in that code no closure endures for any length of time. The function is created, called, and then released; nothing maintains a reference to it, so it doesn't live on, and the lexical environment object it referenced can be reclaimed. Creating an object in a function does not give the object a reference to the function (or the environment object), so the function isn't retained, and so it doesn't retain its enclosing environment in memory.

Contrast with:

(function outer(x) {
    setTimeout(function inner() {
        alert(x);
    }, 100);
})("foo");

There, we create two closures (outer and inner) but outer is released almost immediately (like your example); inner is released 100ms or so later after the timer fires and the timer subsystem releases its reference to it.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

getContact is a IIFE ( immediately-invoked function expression) which returns an object. Here no inner function is created which refers to the outer function's environment variable. It should not be considered closure in my opinion, still waiting for explanation which can justify this as a closure.

Rafique Ahmed
  • 117
  • 2
  • 8
  • What I understood is, that technically every function is a closure, but if you don't return an "inner function" the closure is not persisted after the function execution ends. A function has a private scope == "closure"?! Actually I think, "closure" == "persisted private scope of a function", that is what is commonly understood as closure. I hope I could explain it well. – Legends Jan 07 '17 at 12:40