Search code examples
javascriptscopenomenclature

Is the following JavaScript construct called a Closure?


(function() {

  //do stuff

})();

EDIT: I originally thought this construct was called a closure - not that the effect that it caused results (potentially) in a closure - if variables are captured.

This is in no way to do with the behaviour of closures themselves - this I understand fully and was not what was being asked.


Solution

  • It is an anonymous function (or more accurately a scoped anonymous function) that gets executed immediately.

    The use of one is that any variables and functions that are declared in it are scoped to that function and are therefore hidden from any global context (so you gain encapsulation and information hiding).