[英]When I console.log type of my function which returns an object it gives undefined. I don't understand why
I can't understand why the result of this code is undefined? 我不明白为什么这段代码的结果不确定?
function f() {
return
{
x: 0
};
}
console.log(type of f());
typeof
is a single word. typeof
是一个单词。 Also, you are invoking your function, thus you are evaluating it's returned value, which is undefined in your code because of automatic semicolon insertion. 另外,您正在调用函数,因此您正在评估它的返回值,由于自动分号插入,该值在代码中未定义。
from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return 来自https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/return
The return statement is affected by automatic semicolon insertion (ASI).
return语句受分号自动插入(ASI)的影响。 No line terminator is allowed between the return keyword and the expression.
return关键字和表达式之间不允许使用行终止符。
function f() { return //this is treated as though it had a ; { x: 0 }; } console.log(typeof f()); console.log(typeof f);
function f() { return { x: 0 }; } console.log(typeof f()); console.log(typeof f);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.