An easy mistake topic

What Will the following code prints the result is? why? 

var b = 10;
(function b() {
  b = 20;
  console.log(b);
})()

It is 10? Or 20? In fact, not

We know that the scope of the upgrade function declarations and variable declarations improved, but here's function b () {} is a function expression , not a function declaration.

Function expression and function declaration is different from its function name only valid within the function, and this binding part of the binding constant.

In the non-self-executing anonymous function, the function name, the function variable is read-only, can not be modified

That is, b = 20 is invalid; print below that b when the first look inside the function, did not find! Then go global to find, find a function.

 

If this function is performed immediately if anonymous function, the result is 20; if applied to a var b = 20 before, the result is 20;

The title code is executed when the strict mode, being given; the non-strict mode, it defaults to b = 20 Invalid

 

 

Published 41 original articles · won praise 5 · views 10000 +

Guess you like

Origin blog.csdn.net/lan128lan/article/details/104044017