Variable promotion: In the current scope, var and function are pre-declared or defined

Note: block-level scopes are only valid for let/const/function, not var

Let’s get started


Variable enhancement block-level scope interview question 1

var a = 0

if(true) {

    console.log(a)

    a = 1

    console.log(a)

    function a (){}

    console.log(a)

    a = 21

    console.log(a)

}

console.log(a)

Copy the code

Print the result

ƒ a(){} 1Copy the code

Analysis of the

Var a, function a

console.log(a)//undefined

var a = 0

if(true) {

    console.log(a)//function a

    a = 1

    console.log(a)/ / 1

    function a (){}// map a to global a=1

    console.log(a)/ / 1

    a = 21// Private, not mapped globally

    console.log(a)/ / 21

}

console.log(a)/ / 1



Copy the code

The function contains parameters

// contains parameters

var a= 10,b=11,c=12;

console.log(test)

function test(a){

    a= 1

    var b =2

    c=3

}

test(5)

console.log(a)

console.log(b)

console.log(c)

Copy the code

Print the result

ƒ test(a){a= 1 var b =2 c=3Copy the code

Analysis of the

Var a, function test(){}

var a= 10,b=11,c=12;

console.log(test)// There is no block-level scope declared and defined

function test(a){

    a= 1 // The parameters passed in are private variables and do not affect public

    var b =2//var declares a private variable for function

    c=3// No var, global

}

test(5)

console.log(a)

console.log(b)

console.log(c)

Copy the code

Variable promotion private scope number three

var n = 0

function a({

    var n = 10

    function b({

        n++

        console.log(n)

    }

    b()

    return b

}

var c = a()

c()

console.log(n)

Copy the code

Print the result

11 of 12 0Copy the code

Analysis of the

// The variable is promoted

//var n ; var c ; function a (){}



var n = 0

function a({

    Var n,function b(){}

    var n = 10

    function b({

        n++ // Operate on its parent scope, not global

        console.log(n)

    }

    b()

    return b

}

var c = a()

c()// call b

console.log(n)Function a operates on 'var n = 10' private variables

Copy the code

The private scope of variable promotion is wrong

var foo = 1

function bar (){

    if(! foo){

        var foo = 10

    }

    console.log(foo)

}

bar()



// Print the result

/ / 10



Copy the code

Variable promotes var foo; Function bar() {} bar() private scope: var foo = undefined! Foo is true, foo = 10, and 10 is printed

Variable promotion without VAR problem 5

console.log(a)
a = 12
function fn(){
    console.log(a)
    a = 13
}
fn()
console.log(a)
Copy the code

A is not defined

Tips: Variable enhancement differences between old and new browsers

Note that the old and new versions treat function under block-level scope differently (block-level scope exists only when braces {} exist).

  • New version: only function is declared in advance, not defined
  • Older version: declared and defined in advance

Example:

console.log(a)

var a = 0

if(true) {

    console.log(a)

    function a(){}

    console.log(a)

}

console.log(a)

Copy the code

New version analysis

  1. Var a, function a
  2. console.log(a)Print a, in which case a is only declared, undefined, undefined
  3. var a = 0I’m going to assign the variable a, a=0
  4. Once in the block-level scope,function a(){}A is not reassigned, but the modified a is mapped to the global for compatibility with older versions, in which case the global A is a function
  5. So all three of these are printedfunction a(){}

The new version of Google prints resultsThe latest version of Internet Explorer 11 is displayed

Old version analysis

Function a (){}

  1. Since the old version ignored block-level scope, function was variable promoted and defined, and the a was function
  2. I defined a to be 0, so a is equal to 0
  3. Enter the block-level scope, the old version to ignore, so print a still is zero, the function a () {} has been defined statement, no longer perform, print a, is still a = 0, a global or a = 0
console.log(a)/ / step 1

var a = 0/ / step 2

if(true) {

    console.log(a)

    function a(){}

    console.log(a)

}

console.log(a)

Copy the code

Internet Explorer 10 and the following results are displayed

conclusion

Block-level scopes are only recognized by new browsers, so they are declared first for compatibility with older browsers, but new browsers do not define them until they enter the block-level scope in which the function is located.

Xiaobian: If all the above questions are correct, the basis of proving variable promotion is very strong. If you do not do the right word, I suggest you review it again. Welcome erratum, very grateful, useful words praise attention oh!

Scan the QR code to follow us!

This article is formatted using MDNICE