Common interview questions in this section

// ------------------
console.log(a)
a = 'Lin Yiyi'
function fn(){
    console.log(a)
    a = 12
}
fn()
console.log(a)

// Uncaught ReferenceError: A is not defined

// ------------------------
var a = 'Lin Yiyi'
function fn(){
    if(! a){var a = 12
    }
    console.log(a)
}
fn()

/ / output 12
// var a = 12 a = true; Var a = 12

// -------------------
var a=12, b = 13, c = 14
function fn(a){
    a = 0
    var b = 0
    c = 0
}
fn(a)
console.log(a)
console.log(b)
console.log(c)

// Output 12, 13, 14
// a is a parameter b is a local variable so it does not change the value of the global variable c changes the value of the global variable

Copy the code

Basic concept

Scope: scope is the access scope and permission of a variable. In which scope a variable can be accessed, this scope is its scope. Globally accessible is the global scope, and only internally accessible is the function body. Es6 introduces block-level scopes.

Variable promotion: Functions and variables declared using VAR are promoted to the top declaration of the scope regardless of where the variable is defined in the code. No error is reported when used before.

Core principles

Scope: Each function has a [[scope]] attribute that points to the set of execution contexts in which the function is executed.

Scope chain: The set of execution contexts to which the [[scope]] attribute points is what we call a scope chain

function test() {
  var a = 'a'
}
var glob = 'global'
test()
Copy the code

The scope chain is formed when the test function is called, and exists in [[scope]]

Variable promotion:

Js code execution process, there will be compilation and execution of two stages.

The compilation stage is not to take all the code and compile it, but to execute some of the code and compile some of the code.

However, due to this short compilation process, the JS engine collects all the variables and declares them and makes them valid. The rest of the statements do not take effect until the execution phase reaches a certain sentence.

For what?

  1. Used to delineate where a variable has access
  2. Can be used as closures

The advantages and disadvantages

Scope:

Advantages:

  1. Giving variables their own access scope makes your code more flexible
  2. The scope chain mechanism enables inheritance
  3. Protects variables of its own scope from variables of other scopes

Disadvantages:

1. Es5 has only functions and global scopes, but no scopes in loops and partial code blocksCopy the code

Surface pilot analysis

console.log(a)
a = 'Lin Yiyi'
function fn(){
    console.log(a)
    a = 12
}
fn()
console.log(a)
Copy the code

Uncaught ReferenceError: A is not defined

The console.log(a) statement will report an error, and the rest of the code will not execute.

var a = 'Lin Yiyi'
function fn(){
    if(! a){var a = 12
    }
    console.log(a)
}
fn()

/ / output 12
// var a = 12 a = true; Var a = 12
Copy the code

When fn() is executed, var a = 12 is declared in advance, regardless of whether it is executed.

If (! In a), a is undefined, so after the if statement, a is a local variable and prints 12

var a=12, b = 13, c = 14
function fn(a){
    a = 0
    var b = 0
    c = 0
}
fn(a)
console.log(a)
console.log(b)
console.log(c)

// Output 12, 13, 0
// a is a parameter b is a local variable so it does not change the value of the global variable c changes the value of the global variable
Copy the code

This body looks at which global variables are overridden when a function is called.

When fn is called, the global a is passed to the function as a parameter, and b is declared with var as a local variable.

Only C is c that directly changes the whole world. So it prints out 12, 13, 0

expand

Closure: Variables in the scope of a function are private variables and cannot be accessed outside the function. However, a closure is formed by accessing variables inside the function through some means.

When a function returned by a return is referenced by another scope, the private variables inside the function are not collected by the garbage collection mechanism

Since the garbage collection mechanism does not collect, memory is consumed.

The inertia function, the Currization function, and the compose function all use the closure mechanism.