JS closure and something related
Closure & emulate Private Scope in JS
A closure can not only access the variables defined in its outer function but also the arguments of the outer function.
The example below creates a private scope by adding parenthesis at the end of the function, it tells the interpreter to execute it as soon as it reads it without invocation. Functions and variables in it are not accessible from outside.
|
|
But what if we want some of the functions to be public?
The Module Pattern
The Module Pattern looks like this:
and can be used like this:
|
|
An even better way of doing this is to add underscore to the private functions and return an anonymous object containg public functions:
|
|
Reference: Understanding Scope in JavaScript