Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Day9:写出执行结果,并解释原因 #45

Open
Genzhen opened this issue Jun 22, 2020 · 2 comments
Open

Day9:写出执行结果,并解释原因 #45

Genzhen opened this issue Jun 22, 2020 · 2 comments

Comments

@Genzhen
Copy link
Collaborator

Genzhen commented Jun 22, 2020

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

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

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

@Genzhen
Copy link
Collaborator Author

Genzhen commented Jun 22, 2020

答案
输出是抛出异常,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

@Dannyzn
Copy link

Dannyzn commented Jul 19, 2020

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

@Genzhen Genzhen closed this as completed Jul 20, 2020
@Genzhen Genzhen reopened this Jul 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants