Home > Web Front-end > JS Tutorial > body text

Talk about my understanding of JavaScript prototypes and closure series (random notes 6)_javascript skills

WBOY
Release: 2016-05-16 15:24:39
Original
1205 people have browsed it

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

Every time the controller switches to ECMAScript executable code, it enters an execution context.


Execution context (EC for short) is an abstract concept. The ECMA-262 standard uses this concept to distinguish it from the concept of executable code.

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.

Copy code The code is as follows:
console.log(this) //Window


Case 3: Function: function expression and function declaration

 console.log(f1); //function f1() {}
function f1() {} //函数声明
console.log(f2); //undefined
var f2 = function() {}; //函数表达式 
Copy after login

Summary of “Preparation”:

•Variable, function expression————Variable declaration, the default assignment is undefined


•this————assignment


•Function declaration————Assignment


We call the preparation of these three types of data "execution context" or "execution context environment".


-------------------------------------------------- ----------------------------------

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涉及到安全问题(脚本注入),所以尽量不用。 
Copy after login
-------------------------------------------------- ----------------------------------

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); 
Copy after login
The above code shows that the arguments variables and function parameters have been assigned before the statements in the function body are executed.


Every time the function is called, a new context execution environment will be generated. Because different calls may produce different parameters.


The scope of the free variables inside the function body has been determined when the function is defined (not called).

 var a = 10;
function fn() {
 console.log(a); //a是自由变量
}     //函数创建时,就确定了a要取值的作用域
function bar(f) {
 var a = 20;
 f(); //打印"10",而不是"20"
}
bar(fn); 
Copy after login

概要:

グローバル コードのコンテキスト データの内容は次のとおりです:

•通常の変数 (関数式を含む): var a = 10 | ===> 宣言 (デフォルトの割り当ては未定義)

•関数 fn() {} などの関数宣言

•これ | ===>

関数本体

•パラメータ | ===>


•引数 | ===>


•自由変数の値の範囲 | ====> 代入

一般的な定義:


コードを実行する前に、使用する変数をすべて事前に取り出し、いくつかは直接代入され、いくつかは最初に未定義で埋められます。

上記の内容は、編集者が共有した JavaScript プロトタイプとクロージャー シリーズの理解 (ランダムなメモ 6) の全説明です。気に入っていただければ幸いです。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!