Statement: This category belongs to the study notes, mainly extracts the contents of the book, less collation. Content is often jumping, it is recommended that you read books, the benefits are greater.
There are 2 ways to define a function, the first is a function declaration, and the other is a function expression
The function declaration is promoted, that is, the JavaScript engine does nothing first, the function declaration of the Code to parse the first, then you will be used before or after the use of it does not matter, but the expression does not have this feature, must be called after the function expression is not an error.
// this is possible. sayhi (); function Sayhi () { alert ("hi");} // This is not going to work. Sayhi (); var sayhi=function () { alert ("hi");};
About recursion: recursive because the function name may change, so generally does not use the function name, but uses the Arguments.collee (n-1) similar method, but in strict mode, cannot obtain the arguments, therefore can do so
// Arguments.callee cannot be accessed through scripting in strict mode // You can use the following methods instead of using the var fac= (function f (num) { if(num<=1) { return 1; } Else { return num*f (num-1); }}); Console.log (FAC (5));
Closed Package
Functions that have access to variables in another function scope
Common ways to create closures use nested functions
When the function is first called, an execution environment and the corresponding scope chain are created, and the scope chain is assigned to a special internal property [[scope]]. Then use the values of this,arguments and other named functions to initialize the active object of the function. However, the active object of the external function is in the second position, so that it continues to the global execution environment.
Each execution environment in the background has an object that represents a variable-the variable object. Variable objects in the global environment are always present, and the variable objects of the local environment within the function disappear after the function execution is finished. A scope chain is essentially a list of pointers to variable objects that reference but do not actually contain variable objects.
However, a function defined internally by a closure adds the active object containing the function (external function) to its scope chain. When an external function (not a global function) finishes executing, the scope chain of its execution environment is destroyed, but the active object is not recycled.
Because the closure carries the scope of the function that contains it, it consumes more memory than other functions, and excessive use of closures can lead to excessive memory consumption.
Secondly
The configuration mechanism of the scope chain causes a closure side effect:
Only the last value that contains any of the variables in the function can be obtained.
function Createfunc () { var result=New Array (); for (var i=0; i<10; i++) { result[i]=function() { return i; }; } return result;} // the resulting function array, I all ten
Because the active objects of external functions are kept in the scope chain of each function, they refer to the same variable i, when the function returns the value of Result,i is 10.
We can use a way to force closures to meet our expectations.
functionCreate () {varresult=NewArray (); for(vari=0; i<10; i++) {Result[i]=function(num) {return function(){ returnnum; }; } (i);//immediately executes the anonymous function and gives the results to result } returnresult;} Console.log (Create ());
In a closure, a problem that often causes problems is this
The execution environment of an anonymous function is global, so this is usually a point to window. This may vary, however, because closures are written in different ways.
var name= "The window" var obj={name: "My object" , Getnamefunc: function () {
return
function () { return this .name; }; }};console.log (Obj.getnamefunc ()); // the window
var name= "the Window"; var obj={ name:"My object", getnamefunc:function() { var that=this returnfunction() { return that.name; }; // My Object
In the following special cases this may also change unexpectedly
varName= "the Window"; varobj={name:"My Object", Getnamefunc:function(){ return This. Name; } }; Console.log (Obj.getnamefunc ()); //The object varAa= (Obj.getnamefunc) ();//The object varBb= (Obj.getnamefunc=obj.getnamefunc) ();//The windowConsole.log (aa+ "" +b); //The first line of code and the usual method of invoking the object //the second line of code uses the immediate execution //the third line executes the assignment statement and then invokes the assignment result. Because the assignment expression is the function itself //so this cannot be maintained and the result is returned to This.window
JavaScript Advanced Programming (second Edition) learning (5) Function expressions