Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Misunderstanding of JavaScript closures

I'm new to JavaScript, and am having a problem understanding this code:

function addProperty(o) {
   var value;

   o["get"] = function()  { return value; }
   o["set"] = function(v) { value = v; }
}

var a = {};
addProperty(a);
var b = {};
addProperty(b);

a.set(4);
b.set(5);
print("a is " + a.get() + "; b is " + b.get());

This prints (in v8/d8) a is 4; b is 5. If I comment out the var value; line, I get a is 5; b is 5. Where is the 'value' object, and why are there two of them? Thanks.

like image 524
EML Avatar asked Jan 04 '12 15:01

EML


People also ask

What are the disadvantages of closures in JavaScript?

Disadvantages of closures There are two main disadvantages of overusing closures: The variables declared inside a closure are not garbage collected. Too many closures can slow down your application. This is actually caused by duplication of code in the memory.

Are JavaScript closures important?

Closures are important because they control what is and isn't in scope in a particular function, along with which variables are shared between sibling functions in the same containing scope.

What could be the alternative to closure in JavaScript?

Top Alternatives to Closure CompilerA bundler for javascript and friends. Packs many modules into a few bundled. Code Splitting allows to load parts for the application on demand. Through "loaders" modules can be CommonJs, AMD, ES6 modules, CSS, Images, JSON, Coffeescript, LESS, ... and your custom stuff. ...

What would happen if I remove the closure feature from JavaScript?

If JavaScript did not have closures, then more states would have to be passed between functions explicitly, making parameter lists longer and code noisier. So, if you want a function to always have access to a private piece of state, you can use a closure.


2 Answers

The value variable is local to addProperty. The first time addProperty is called, a new value is created, over which both of the functions close. The second time addProperty is called, a second value is created over which two new functions close.

Removing var creates a global value on the window object that is shared by all of the functions.

Maybe you mean to do this:

function createPropertyMgr() {
    var value;
    return function(o) {
       o["get"] = function()  { return value; }
       o["set"] = function(v) { value = v; }
    }
}

var addProperty = createPropertyMgr();

This new addProperty function closes over one value no matter how many times it's called. I'm not sure I understand the use case, but that should demonstrate the difference.

like image 183
Wayne Avatar answered Sep 19 '22 04:09

Wayne


When you don't explicitly declare a variable inside a function, its scope is assumed as global. i.e. in your second case, since you have not declared value explicitly as local to the function addProperty(), it gets treated as global.

But when you do declare it explicitly inside a function, it becomes local to the function. Every time the function is invoked a new copy of the function local variable is created on the stack (and importantly it stays there in the stack as long as there is a reference to it)

like image 28
techfoobar Avatar answered Sep 22 '22 04:09

techfoobar