1. function expression 1.1 function definition
There are two ways to define a function: One is a function declaration and the other is a function expression.
(1). function declaration: An important feature of a function declaration is a function declaration hint, which executes before a function executes
function functionname (ARG0,ARG1,ARH2) {
function body
}
(2). function expressions
var functionname = function (ARG0,ARG1,ARH2) {
function body
}
1.2 Recursion
A recursive function is formed when a function calls itself by name, for example:
function factorial (num) { if (num <= 1) { return 1; } else { return num * factorial (num-1); } } |
But the following code causes an error,
var anotherfactorial = factorial; factorial = null; Alert (Anotherfactorial (4)); Error |
Arguments.callee is a pointer to an executing function that can be used to implement a recursive call to a function
function factorial (num) { if (num <= 1) { return 1; } else { Return num * Arguments.callee (NUM-1); } } |
In strict mode, the Arguments.callee call fails, and you can use a named function expression to achieve the same result
var factorial = (function f (num) { if (num <= 1) { return 1; } else { Return num * f (num-1); } }) |
1.3 Closures
Closures refer to functions that have access to variables in another function scope. The most common way to create closures is to create another function inside one function.
1.3.1 Closures and variables
Closures can only get the last value of any variable in the containing function
function Createfunctions () { var result = new Array (); for (var i = 0; i<10;i++) { Result[i] = function () { return i; } }; return result; } |
Each of the above functions has a value of 10, in order for the function to return the correct value, you can use the anonymous function to force the closure to conform to the expected behavior.
function Createfunctions () { var result = new Array (); for (var i = 0; i < i++) { Result[i] = function (num) { return function () { return num; } } (i); }; return result; } |
1.3.2 about this object
The This object is bound at run time based on the execution environment of the function, in the global variable, this equals window, and when the function is called as a method of an object, this is equal to that object, but the execution environment of the anonymous function is global, so its this usually points to window.
1.4 Impersonation block-level scopes
There is no concept of block-level scope in JavaScript, and anonymous functions can be used to mimic block-level scopes.
The syntax for an anonymous function that mimics a block-level scope (often referred to as a private scope) is as follows:
(function (params) { Block-level scopes }) (params) |
1.5 Private variables
Strictly speaking, there is no concept of private members in JavaScript, all object properties are public, but variables defined in any function are private, and private variables are implemented by defining variables in the function.
var obj = function () { var privatevar = ten; function Privatefunc () { return false; } return { publicproperty:true . publicmethod:function () { privatevar++; return Privatefunc (); } } |
7. Javacript Advanced Programming-function expressions