What is the purpose of a self executing function in javascript?

It’s all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of JavaScript code. For example, as mentioned in a comment by Alexander: This will first log 3 and … Read more

Don’t understand why UnboundLocalError occurs (closure) [duplicate]

Python doesn’t have variable declarations, so it has to figure out the scope of variables itself. It does so by a simple rule: If there is an assignment to a variable inside a function, that variable is considered local.[1] Thus, the line implicitly makes counter local to increment(). Trying to execute this line, though, will try to read the value of … Read more

Static variables in JavaScript

If you come from a class-based, statically typed object-oriented language (like Java, C++ or C#) I assume that you are trying to create a variable or method associated to a “type” but not to an instance. An example using a “classical” approach, with constructor functions maybe could help you to catch the concepts of basic OO JavaScript: … Read more