The last time I saw the 6th chapter, object oriented. This is followed by the 7th chapter.
7th chapter: Function expressions
There are two ways of defining a function: function declaration, function expression
function declaration functions functionname (ARG0,ARG1,ARG2) {//code ...} function expression var functionname = function (arg0,arg1,arg2) {//code ...};
One important feature of function declarations is the function declaration elevation . is to read the function declaration before executing the code, which means that the function declaration can be placed after the statement that called it.
function declaration elevation Sayhi (); Hellofunction Sayhi () { alert ("Hello");}
The function expression looks like a regular variable assignment statement. That is, create a function and copy it to the variable functionname. The function created in this case is called an anonymous function . The anonymous function you create can be assigned to a variable, or it can be returned as a value for other functions.
function expressions must be assigned before they are called.
function expression has no function to promote sayhi (); Errorvar sayhi = function () { alert ("Hello");};
1. recursion
A recursive function is formed when a function calls itself by name.
Recursive function: Name function factorial (num) { if (num <= 1) { return 1; } else { return num * factorial (num-1);
}}var anotherfactorial = factorial;factorial = Null;alert (anotherfactorial (4)); error!//recursive function: arguments.calleefunction factorial (num) { if (num <= 1) { return 1; } else { return num * Arguments.callee (NUM-1); }} var anotherfactorial = Factorial;factorial = Null;alert (anotherfactorial (4)); 24
Use Arguments.callee instead of the function name to make sure that no matter how the function is called.
In strict mode ("use strict"), Arguments.callee cannot be accessed. You can use function expressions to resolve problems.
"Use strict"//recursive function: expression var factorial = function f (num) { if (num <= 1) { return 1; } else { return nu M * F (num-1); }}
2. closures
closure refers to a function that has access to a variable in another function scope. a common way to create closures is to create another function inside one function. ( closures).
When a function call creates an execution environment and the corresponding scope chain , and assigns the scope chain to a special internal property ([[scope]]). the active object of the function is then initialized with the values of this, arguments, and other parameters that are clearly arguments. In a scope chain, the active object of an outer function is always second, and the outer function of the external functions of the active object is in the third position ... The global execution environment to the end of the scope chain.
During function execution, to read and write the value of a variable, you need to look for the variable in the scope chain.
function Compare (value1,value2) { if (value1 < value2) { return-1; } else if (value1 > value2) { return 1; } else{ return 0;} } var result = compare (5,10);
The Compare () function is defined before it is called in the global scope. Call Compare () to create an active object that contains this, arguments, value1, value2. The variable object (this, result, compare) of the global execution environment is second in the scope chain of the Compare () execution environment.
In a scope chain, a closure can only get the last value that contains any variable in the function. Closures save the entire variable object, not a particular variable.
function Createfunctions () { var result = new Array (); for (var i = 0; i < 5; i++) { Result[i] = function () { return i; }; } return result;} var arr = createfunctions (); Console.log (Arr[0] ())//5console.log (Arr[1] ())//5console.log (Arr[2] ())//5console.log (Arr[3] ())//5console.log ( ARR[4] ())//5function createFunctions2 () { var result = new Array (); for (var i = 0; i < 5; i++) { Result[i] = function (num) { return function () { return num; } } (i); } return result;} var arr2 = CreateFunctions2 (); Console.log (Arr2[0] ())//0console.log (Arr2[1] ())//1console.log (Arr2[2] ())//2console.log (Arr2[3] ())// 3console.log (Arr2[4] ())//4
Memory leaks
Add:
execution Environment: each time a function is called (when executing a function), a closed, local run environment is created for the function, that is, the function's execution environment. Functions are always executed in their own execution environment, such as reading and writing local variables, function parameters, and running internal logic. The process of creating an execution environment includes the scope of the creation of the function, which is also performed under its own scope. From another perspective, each function execution environment has a scope chain, and the scope chain of the child function includes the scope chain of its parent function.
Scope , scope chain , calling object: Function scopes are divided into lexical scopes and dynamic scopes. The lexical scope is the scope of the function definition, that is, the static scope. When a function is defined, his lexical scope is determined, and the lexical scope illustrates the scope of function under the nested relationship of the function structure. This time also formed the scope chain of the function. The scope chain is the concatenation of these scopes with nested hierarchies. The internal [[scope]] property of the function points to the scope chain. a dynamic scope is the scope at which a function call executes. When a function is called, the function's internal [[scope]] property is first pointed to the scope chain of the function, then a calling object is created and the call object is used to record the function arguments and local variables of the function, placing them at the top of the scope chain. A dynamic scope is created by adding the call object to the top of the scope chain, where [[scope] has a scope chain that is defined, and a call object that is created when the call is made. In other words, a new scope chain is formed by the scope chain that is determined when the scope of the execution environment equals the definition of the function, plus the call object that the function has just created. So it's a dynamic scope, and the scope chain changes with it. The scope here is actually an object chain, which is the invocation object created at the time of the function call, as well as the top-level global object of the call object above it.
3. block-level scopes
In JavaScript, there is no block-level scope concept. You can use anonymous functions to mimic block-level scopes.
var functionname = function () { //block-level scope} (function () { //block-level scope});(function () { //block-level scope} ());
4. Private Variables
In JavaScript, there is no concept of a private member. any variable defined in the function can be used as a private variable (parameter of the function, local variable, intrinsic function).
There are 3 private variables in the Add function: Num1 num2 Num. function Add (num1,num2) { var num = num1 + num2; return num;}
This chapter introduces recursive functions, closures, scopes, and so on. understand the concepts of closures, scope chains, and execution environments.
Reading time "JavaScript Advanced Programming" Three: Functions, closures, scopes