Fe-interview: Day9:写出执行结果,并解释原因

Created on 22 Jun 2020  ·  2Comments  ·  Source: lgwebdream/FE-Interview

var foo = function bar(){ return 12; };
console.log(typeof bar());  
// 写出执行结果,并解释原因

每日一题会在下午四点在交流群集中讨论,五点小程序中更新答案
欢迎大家在下方发表自己的优质见解
二维码加载失败可点击 小程序二维码

扫描下方二维码,收藏关注,及时获取答案以及详细解析,同时可解锁800+道前端面试题。

Most helpful comment

typeof(bar). // "undefined"
typeof(foo()). // "number"
typeof(foo).   // "function"
typeof(bar()). // VM5167:1 Uncaught ReferenceError: bar is not defined

All 2 comments

答案
输出是抛出异常,bar is not defined。

解析
这种命名函数表达式函数只能在函数体内有效

var foo = function bar(){ 
    // foo is visible here 
    // bar is visible here
    console.log(typeof bar()); // Work here :)
};
// foo is visible here
// bar is undefined here
typeof(bar). // "undefined"
typeof(foo()). // "number"
typeof(foo).   // "function"
typeof(bar()). // VM5167:1 Uncaught ReferenceError: bar is not defined
Was this page helpful?
0 / 5 - 0 ratings