为什么这个函数返回undefined?

-1 javascript

我不会理解为什么以下函数不返回{bar:"hello"}而是返回undefined.

function foo2() {
    return
    {
        bar: "hello"
    };
}
Run Code Online (Sandbox Code Playgroud)

Ami*_*oki 5

这是因为它编译到下面因为javascript的自动分号插入.

function foo2() {
    return; // notice the semi-colon here?
    {
        bar: "hello"
    };
} 
Run Code Online (Sandbox Code Playgroud)

并且自return;调用以来,函数终止而不进入下一行代码.为了使它正常工作,只是把左括号后立即returnreturn {

使用分号而不是省略它们会更好.想要理由吗?查看Javascript自动分号插入的危险