Skip to content

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

@Genzhen

Description

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

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

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

Activity

Genzhen

Genzhen commented on Jun 22, 2020

@Genzhen
CollaboratorAuthor

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

Dannyzn commented on Jul 19, 2020

@Dannyzn
typeof(bar). // "undefined"
typeof(foo()). // "number"
typeof(foo).   // "function"
typeof(bar()). // VM5167:1 Uncaught ReferenceError: bar is not defined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Genzhen@Dannyzn

        Issue actions

          Day9:写出执行结果,并解释原因 · Issue #45 · lgwebdream/FE-Interview