JS syntax scope problem.

recently I have been reading the book on JAVASCRIPT language essence and programming practice.
the grammar scope is discussed in the book

clipboard.png
clipboard.png

clipboard.png
program reports an error directly, func1 is not a function. The
function func2 can be executed because the function has been enhanced.
so what"s the problem? Guys.

Mar.13,2021

it just has something to do with the time of the person who wrote the book, and some books will tell you that the rule is changing. What is hidden here is the promotion of the function declaration.

the following is the ECMAScript standard, which is not recommended under any circumstances for the purpose of explaining appeal errors only.

Function statements are NOT declared during variable instantiation. They are declared at run time, just like function expressions. However, once declared, function statement's identifier becomes available to the entire scope of the function. This identifier availability is what makes function statements different from function expressions (you will see exact behavior of named function expressions in next chapter).
console.log(typeof foo); // "undefined"
if (true) {
  // once block is entered, `foo` becomes declared and available to the entire scope
  function foo() { return 1; }
} else {
  // this block is never entered, and `foo` is never redeclared
  function foo() { return 2; }
}
console.log(typeof foo); //

http://kangax.github.io/nfe/-sharp.


say simple, say complex
you put the definition of func1 after judgment, the same is true in the example, here involves the question of which kind to execute first

if(true){
    function func1(){
        console.log(56)
    }
}
func1()

clipboard.png
http://www.jb51.net/article/9.

Menu