有人可以帮助我 - 为什么我们在 JS 片段中有这种行为?
var foo = function() {
return {
hi: console.log("foo")
}
}
var foo1 = function() {
return
{
hi: console.log("foo1")
}
}
foo();
foo1();
为什么只打印“foo”?
fiddle https://jsfiddle.net/apimenov93/gxysuxvc/
EDIT好的,这是因为自动插入分号,但是
我们有什么方法可以强制 JS 不执行这种情况吗?
我的意思是,我们可以做一些会在这里引发错误的事情吗?
EDIT2
看起来最好的建议是 JShint - 我问here https://stackoverflow.com/questions/35987848/jshint-strange-behavior
你已经接触到了 JavaScript自动插入分号 https://stackoverflow.com/questions/2846283/what-are-the-rules-for-javascripts-automatic-semicolon-insertion-asi。你的第二个块相当于:
var foo1 = function(){
return;
{
hi:console.log("foo1")
}
}
IE 它根本不返回对象文字(所以console.log
没有运行) - 它只是返回undefined
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)