how could I pass closure problems in order to increment a global var

Posted by hyptos on Stack Overflow See other posts from Stack Overflow or by hyptos
Published on 2013-06-27T08:57:03Z Indexed on 2013/06/27 10:21 UTC
Read the original article Hit count: 200

Filed under:

I have a simple goal, I would like to increment a variable but I'm facing the closure problem. I've read why this s happening here How do JavaScript closures work?

But I can't find the solution to my problem :/

let's assume this part of code I took from the link.

function say667() {
// Local variable that ends up within closure
var num = 666;
var sayAlert = function() { alert(num); //incrementation
}
num++;
return sayAlert;
}

I would like to increment num within the function and to keep the changes to num.

How could I do that ?

Here is the JsFiddle where I have my problem, I can't figure out how to increment my totalSize and keep it.

http://jsfiddle.net/knLbv/2/

I don't want a local variable that ends up with closure.

© Stack Overflow or respective owner

Related posts about JavaScript