Function Expression itself cannot assign Name to another Value

穿精又带淫゛_ 提交于 2019-12-05 16:20:12

Hoisting separates the actual assignment from the variable declaration. What it's really doing is this:

(function (){
        var test, printTest;
        test = function (){};
        test = 1;//assign value to a variable here
        printTest = typeof test;
        document.write(printTest);
    })();

var test only means "Anything called test should be scoped locally". It is undefined only because you haven't assigned a value to it (except you have with function test(){}; which is why you get function and not undefined).

In the second example, function test(){}; still assigns a function to it, but then var test=1; overwrites that with a 1. You use typeof after you assign the 1 to it, so it reports that it is a number.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!