JS奇怪的行为 [英] JS Strange behavior
问题描述
有人可以帮助我 - 为什么我们在JS片段中有这种行为?
Can someone assist me - why we have this behavior in JS snippet?
var foo = function() {
return {
hi: console.log("foo")
}
}
var foo1 = function() {
return
{
hi: console.log("foo1")
}
}
foo();
foo1();
为什么只打印foo?
编辑
好吧,这是因为自动分号插入,但是
EDIT ok, this is because of automatic semi-colon insertion, BUT
我们有办法强迫JS不执行这种情况?
do we have some ways to force JS to not execute this cases?
我的意思是,我们可以做一些会在这里抛出错误的东西吗?
I mean, can we do something that will throw error here?
EDIT2
看起来最好的建议是JShint - 我问过这里
Looks like best suggestion is JShint - i asked here
推荐答案
你已经点击了JavaScript 自动分号插入。你的第二个块相当于:
You've hit JavaScript's automatic semi-colon insertion. Your second block is the equivalent of:
var foo1 = function(){
return;
{
hi:console.log("foo1")
}
}
IE它根本没有返回对象文字(因此 console.log
没有运行) - 它只是返回 undefined
。
IE it's not returning the object literal at all (and so the console.log
isn't running) - it's just returning undefined
.
这篇关于JS奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!