我有这个javascript代码无法正常工作.
function myfun()
{
return
{
alert("para");
}
};
myfun();
Run Code Online (Sandbox Code Playgroud)
我已经阅读了javascript的自动分号插入.我将上面的代码更正为
return{
alert("para");
}
Run Code Online (Sandbox Code Playgroud)
但仍然有错误:unexpected token (.我想知道为什么?
注意:我不需要解决方案,但我需要解释为什么上面的代码不起作用.
编辑
根据书中,javascript的好部分,return语句如果返回值必须与返回表达式在同一行.
即
return {
status: true
};
Run Code Online (Sandbox Code Playgroud)
和
return
{
status: true
};
Run Code Online (Sandbox Code Playgroud)
是错的.然后怎么样
function myfun(para)
{
var status;
return
{
status : alert(para)
};
};
myfun("ok");
Run Code Online (Sandbox Code Playgroud)
产生没有错误.它不会工作,但也没有显示任何错误{.它与返回时在同一行.
即
function myfun(para)
{
var status;
return{
status : alert(para)
};
};
myfun("ok");
Run Code Online (Sandbox Code Playgroud)
在
return {
alert("para");
}
Run Code Online (Sandbox Code Playgroud)
该{...}被解释为对象常量.对象文字具有表单
{
key: value,
//...
}
Run Code Online (Sandbox Code Playgroud)
对象文字不能包含任意语句.