function test()
{
return
{ /* <--- curly brace on new line */
javascript: "fantastic"
};
}
var r = test();
try {
alert(r.javascript); // does this work...?
} catch (e) {
alert('no - it broke: ' + typeof r);
}
result: "no - it broke: undefined"
function test()
{
return { /* <---- curly brace on same line */
javascript: "fantastic"
};
}
var r = test();
try {
alert(r.javascript); // does this work...?
} catch (e) {
alert('no - it broke: ' + typeof r);
}
result :"fantastic"
这是JavaScript的陷阱之一:自动分号插入。 不以分号结尾但可能是语句结尾的行会自动终止,因此第一个示例实际上是这样的:
function test()
{
return; // <- notice the inserted semicolon
{
javascript: "fantastic"
};
}