This article mainly introduces my understanding of the JavaScript prototype and closure series (note 6). For more information, see
Related reading: About my understanding of the JavaScript prototype and closure series (note 8) about my understanding of the JavaScript prototype and closure series (note 9)
What is a closure?
What is a closure? The Closure is Closure, which is a new feature not available in static languages. However, closures are not complicated to be incomprehensible. In short, closures are:
• Closure is the set of local variables of the function, but these local variables will continue to exist after the function returns.
• Closures are the "stacks" of functions that are not released after the function is returned. We can also understand that these function stacks are not allocated on stacks but on stacks.
• When defining another function in a function, a closure is generated.
What is prototype?
A prototype is an object that can be used to inherit attributes.
Can any object be prototype?
Yes
Objects with the original type
All objects have a prototype by default. Because the prototype itself is also an object, each prototype itself has a prototype (with only one exception, the default object prototype is at the top of the prototype chain ).
Execution Context
Each time the controller is transferred to ECMAScript executable code, it enters an execution context.
Execution context (-EC) is an abstract concept, which is used by ECMA-262 standards to distinguish it from executable code.
The standard specification does not accurately define the EC type and structure from the perspective of technical implementation; this should be an issue to be considered when implementing the ECMAScript engine.
The execution context of the activity is logically grouped into a stack. The bottom of the stack is always a global context, and the top of the stack is the current (active) Execution context. The stack is modified when a variable of the EC type (varous kingds of EC) is pushed or popped up.
--------------------------------------------------------------------------------
Scenario 1:Before a piece of js code can be used to execute a single sentence, the browser has already done some "Preparations", including the declaration of variables, rather than assigning values. Variable assignment is performed when the assignment statement is executed.
Scenario 2:In the "Preparation" phase, this is directly assigned a value.
The Code is as follows:
Console. log (this) // Window
Case 3:Function: function expression and function declaration
Console. log (f1); // function f1 () {} function f1 () {} // function declaration console. log (f2); // undefinedvar f2 = function () {}; // function expression
Summary of "Preparations:
• Variable and function expression ---- variable declaration. The default value is undefined.
• This ---- value assignment
• Function declaration ---- assign values
The preparation of these three types of data is called "execution context" or "execution context ".
--------------------------------------------------------------------------------
Javascript performs these "Preparations" to generate the execution context before executing a code segment. This "code segment" is divided into three types: Global Code, function code, and Eval code.
// Global code segment