JavaScript Note 5: function expressions
1. function expressions
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 function being executed, so it can be used for recursion.
Function factorial (num ){
If (num <= 1) {return 1 ;}
Else {return num * rguments. callee (num-1 );}
}
3. Closure: Refers to a function that has the right to access the variables in another function scope. The common form of creating a closure is to create another function within one function.
In the background execution environment, the scope of a closure includes its own scope, function scope, and global scope.
When a function returns a closure, the function scope will remain in the memory, knowing that the closure does not exist.
4. Private variables: Any variables defined in the function can be considered as variables because they cannot be accessed outside the function, private variables include function parameters, local variables, and other functions defined within the function.
JavaScript does not have the formal concept of private object attributes, but you can use closures to implement public methods and access variables in its scope through public methods.
The public method that has the permission to access private variables is called the privileged method.
Creating a closure must maintain an additional scope. Excessive use of the closure may occupy a large amount of memory.
Http://www.bkjia.com/kf/201412/359001.html previous