1. Function expression
var function = function () {
function body
};
2. Recursion
function factorial (num) {
if (num<=1) {return 1; }
else {return num*factorial (num-1);}
}
Arguments.callee is a pointer to the executing function, so you can use it to implement recursion.
function factorial (num) {
if (num<=1) {return 1; }
else {return Num*rguments.callee (num-1);}
}
3. Closure: Refers to a function that has access to variables in another function scope, and the common form of creating closures is to create another function inside one function.
The scope of the closure in the background execution environment contains his own scope, the scope of the function and the global scope
When a function returns a closure, the scope of the function is always in memory, knowing that the closure does not exist.
4. Private variables: Any variable defined in a function can be considered to be a variable, since these variables cannot be accessed outside the function, and private variables include parameters of the function, local variables, and other functions defined inside the function.
There is no formal concept of private object properties in JavaScript, but you can use closures to implement public methods to access variables in their scope through public methods
Public methods that have access to private variables are called privileged methods
Creating closures must maintain additional scopes, and excessive use may result in large memory consumption.
JavaScript Note five: function expressions