Related reading: Talk about my understanding of the JavaScript prototype and closure series (Shuishou Note 8)
What is a closure
What is a closure? Closure is Closure, which is a new feature that static languages do not have. But closure is not something too complicated to understand. In short, closure is:• Closure is a collection of local variables of a function, but these local variables will continue to exist after the function returns.
• Closure means that the "stack" of a function is not released after the function returns. We can also understand that these function stacks are not allocated on the stack but on the heap
• When defining another function within a function, a closure will be generated
What is a prototype?
A prototype is an object through which other objects can implement property inheritance.
Can any object become a prototype?
Yes
Which objects have prototypes
All objects have a prototype by default, because the prototype itself is also an object, so each prototype itself has a prototype (with one exception, the default object prototype is at the top of the prototype chain).
Execution context
The standard specification does not accurately define the type and structure of EC from a technical implementation perspective; this should be an issue to be considered when specifically implementing the ECMAScript engine.
Activity execution contexts logically form a stack. The bottom of the stack is always the global context, and the top of the stack is the current (active) execution context. The stack is modified as various kingds of EC are pushed or popped.
Situation 1: Before taking a piece of js code and actually running it sentence by sentence, the browser has already done some "preparatory work", which includes declaring variables instead of assigning values. Variable assignment is performed when the assignment statement is executed.
Case 2: In the "preparation" phase, this is assigned directly.
Case 3: Function: function expression and function declaration
console.log(f1); //function f1() {} function f1() {} //函数声明 console.log(f2); //undefined var f2 = function() {}; //函数表达式
Summary of “Preparation”:
Before JavaScript executes a code segment, it will perform these "preparatory work" to generate an execution context. This "code segment" is divided into three situations-global code, function code, and Eval code.
//全局代码段 <script type="text/javascript"> //代码段... </script> //函数代码段 function fn(x) { console.log(x + 5); } var fn = new Function("x", "console.log(x + 5)"); //Eval代码段 eval('var x = 10'); (function foo() { eval('var y = 20'); })(); alert(x); //10 alert(y); //"y" is not defined //因为eval涉及到安全问题(脚本注入),所以尽量不用。
In the function, in addition to several situations of "preparation", there will also be other data
function fn(x) { console.log(arguments); //[10] conosole.log(x); //10 } fn(10);
var a = 10; function fn() { console.log(a); //a是自由变量 } //函数创建时,就确定了a要取值的作用域 function bar(f) { var a = 20; f(); //打印"10",而不是"20" } bar(fn);
概要:
グローバル コードのコンテキスト データの内容は次のとおりです:
•通常の変数 (関数式を含む): var a = 10 | ===> 宣言 (デフォルトの割り当ては未定義)
•関数 fn() {} などの関数宣言
•これ | ===>
関数本体
•パラメータ | ===>
•自由変数の値の範囲 | ====> 代入
コードを実行する前に、使用する変数をすべて事前に取り出し、いくつかは直接代入され、いくつかは最初に未定義で埋められます。
上記の内容は、編集者が共有した JavaScript プロトタイプとクロージャー シリーズの理解 (ランダムなメモ 6) の全説明です。気に入っていただければ幸いです。