Function names defined as parameters to a function call aren't hoisted. Why not?

前端 未结 3 1921
灰色年华
灰色年华 2021-01-19 03:52

Consider the following code.



Notice that a

3条回答
  •  旧时难觅i
    2021-01-19 04:37

    function b() doesn't exist until the call a(function b() {}); is executed.

    JS doesn't search the code for functions, that way. Also, function b() {} isn't a function declaration, you're merely passing it as a parameter. The only way to access that function would be within function a():

    function a() {console.log(arguments[0])}
    a(function b() {});
    //function b() {}
    

    However, this does not mean that you can actually call b() inside a(), since b() is defined in the scope of a's function call, not in the function itself. Basically, the name b is useless.

提交回复
热议问题