JS奇怪的行为

ale*_*xey 5 javascript

有人可以帮助我 - 为什么我们在JS片段中有这种行为?

var foo = function() {
    return {
        hi: console.log("foo")
    }
}

var foo1 = function() {
    return 
    {
        hi: console.log("foo1")
    }
}

foo();
foo1();
Run Code Online (Sandbox Code Playgroud)

为什么只打印"foo"?

小提琴

编辑 确定,这是因为自动分号插入,但是

我们有办法强制JS不执行这种情况吗?

我的意思是,我们可以做一些会在这里抛出错误的东西吗?

EDIT2

看起来最好的建议是JShint - 我问这里

Jam*_*rpe 8

你已经点击了JavaScript的自动分号插入.你的第二个块相当于:

var foo1 = function(){
  return;
  {
    hi:console.log("foo1")
  }
}
Run Code Online (Sandbox Code Playgroud)

IE它根本没有返回对象文字(因此console.log它没有运行) - 它只是返回undefined.