例如
function a(){
return {
testing:1
};
}
function b(){
return
{
testing:1
};
}
在上面的代码中,函数a()返回一个{testing:1}的对象,但函数b()返回值undefined。这种行为背后的原因是什么?
是不是因为返回值是从第二行开始的?
最佳答案
The
return
statement is affected by automatic semicolon insertion (ASI). No line terminator is allowed between the return keyword and the expression.block 引用>To avoid this problem (to prevent ASI), you could use parentheses:
return ( //return statement. );
function a(){ return ({ testing:1 }); } function b(){ return( { testing:1 }); } console.log(a()); console.log(b());
关于javascript - 从函数返回对象未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52088542/