<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>新建文件夹</title>
</head>
<body>
<script>
function foo1() {
return{
bar: "hello"
};
}
console.log(foo1());
function foo2() {
return
{
bar: "hello"
};
}
console.log(foo2());
</script>
</body>
</html>
在编程语言中,基本都是使用分号(;)将语句分隔开,这可以增加代码的可读性和整洁性。而在 JS 中,如若语句各占独立一行,通常可以省略语句间的分号(;),JS 解析器会根据能否正常编译来决定是否自动填充分号:
var test = 1 + 2;
console.log(test); //3
在上述情况中,为了正确解析代码,就不会自动填充分号了,但是对于 return 、break、continue 等语句,如果后面紧跟换行,解析器一定会自动在后面填充分号(;),所以上面的第二个函数就变成了这样:
function foo2() {
return;
{
bar: "hello"
};
}
所以第二个函数是返回 undefined。