JS (scopes and closures)

1. Understanding of Variable Lifting

2. Explain several different usage scenarios of this

3. Create 10 <a> tags, and the corresponding serial numbers will pop up when clicked

4. How to understand scope

5. Application of closures in actual development

 

1. Execution context

1. Scope: a <script> or a function ( both generate an execution context )

2. Global: variable definition, function declaration  (a <script> first takes out the variable definition and function declaration)

3. Function: variable definition, function declaration, this, arguments  (before a function is executed, the variable definition, function declaration, this, arguments are taken out)

Function declaration:

 Take out the whole function, so the output of function fn(name) is 'zhangsan' 20  

Function expression:

First take out the variable definition, that is, take out a, so console.log(a) outputs undefined (replacement)

 (This is not recommended, put the definition var a = 100 first)

2. this

The value of this must be confirmed when it is executed (there is a () after the function), and it cannot be confirmed when it is defined

Common usage

1. Execute as a constructor

2. Execute as an object property

3. Execute as a normal function (this === window)

4.call apply bind

3. Scope

Fourth, the scope chain

5. Closure

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325326258&siteId=291194637