[英]Javascript function returns an object but the answer is undefined
I'm doing this quiz question http://davidshariff.com/js-quiz/#q=2 The question is:我正在做这个测验问题http://davidshariff.com/js-quiz/#q=2问题是:
function aaa() { return { test: 1 } } console.log(typeof aaa())
The prompted answer for console.log is undefined but I thought it should be an 'object' console.log 的提示答案未定义,但我认为它应该是一个“对象”
Can anyone explain why?谁能解释为什么?
You haven't copied the code correctly, indentation is important here.您没有正确复制代码,缩进在这里很重要。 The original code is原始代码是
function aaa() {
return
{
test: 1
};
}
alert(typeof aaa());
As you can see there's a new line after return
, which after automatic semicolon insertion (ASI), looks as follows:如您所见,在return
之后有一个新行,在自动分号插入(ASI) 后,如下所示:
function aaa() {
return;
{
test: 1
};
}
alert(typeof aaa());
So the return result of this function will be undefined
, since there's no return value and the code is unreachable after the return
statement.所以这个函数的返回结果将是undefined
,因为没有返回值并且代码在return
语句之后是不可访问的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.