1. ScopeSimply put, scope refers to the area in a program where variables are defined, which determines the access rights of the currently executed code to the variables. In ES5, there are generally only two scope types:
After talking about the concept, let's look at the following code: var a = 100 function test(){ var b = a * 2 var a = 200 var c = a/2 console.log(b) console.log(c) } test() // What will be printed here? Analysis:
So it will print NaN, 100 In ES6, a new block scope is added In simple terms, the area within the curly braces // ES5 if(true) { var name = 'Nanjiu' } console.log(name) // Nanjiu // ES6 if(true) { let age = 18 } console.log(age) // This will report an error 2. Scope Chain When accessing a variable inside the executable code, it will first check whether the variable exists in the current scope. If it does, it will return immediately. If not, it will search in the parent scope... until it finds the global scope. We call this scope nesting mechanism 3. Lexical Scope
The so-called lexical scope is determined by where you write the variables and scopes when you write the code. That is, the lexical scope is a static scope, which is determined when you write the code. The scope of a function is determined by where it is declared, not where it is actually called. MDN defines closures as follows: A function is bundled with a reference to its surroundings (lexical environment) (or the function is surrounded by references). Such a combination is called In other words, closures allow you to access the scope of an outer function from within an inner function. In We can conclude that:
Let's look at the code first: var name = 'Front-end Nanjiu' function say() { console.log(name) } say() Analysis: The There is a sentence in the book "Javascript Definitive Guide": Strictly speaking, all But this is just a theoretical closure, which is different from what we usually use. The example above is just a simple closure. ECMAScript defines closures as follows:
Let's look at another piece of code from the JavaScript Definitive Guide: let scope = 'global scope' function checkscope(){ let scope = 'local scope' function f(){ return scope } return f } let s = checkscope() s() // What does this return? Many students may think it is
Basic rules of scope: 5. Application of closure
6. The flaws of closures
7. Frequently asked closure interview questionsvar arr = [] for(var i=0;i<3;i++){ arr[i] = function(){ console.log(i) } } arr[0]() // 3 arr[1]() // 3 arr[2]() // 3 // Here, i has become 3 during execution // Solve using closure var arr = [] for(var i=0;i<3;i++){ arr[i] = (function(i){ return function(){ console.log(i) } })(i) } arr[0]() // 0 arr[1]() // 1 arr[2]() // 2 This is the end of this article about Javascript scope and closure details. For more information about Javascript scope and closure, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Detailed explanation of CSS counter related attributes learning
>>: Docker stop stops/remove deletes all containers
Table of contents 1. Definition of stack 2. JS st...
Table of contents Stabilization Introduction Anti...
This article example shares the specific code of ...
To view the version and tag of the image, you nee...
Recently I saw the article Build your own React o...
1. Solution to the problem that the page is blank...
I just started learning database operations. Toda...
Table of contents 1. Requirements description 2. ...
Mysql supports 3 types of lock structures Table-l...
Recently, when I was working on my "Football...
In react-router, the jump in the component can be...
1. Download centos7 Download address: https://mir...
After installing VMware and creating a new virtua...
The following is my judgment based on the data st...
Preface When creating a primary and foreign key f...