[英]Javascript syntax issue with returning an object literal
var Tweet = Backbone.Model.extend({
defaults: function()
{
return
{
author: ''
status: ''
}
}
});
according to the syntax it should be
根据它应该是的语法
author: '',
status: ''
But this provides an error while the one in code working yet producing no output 但是这会在代码工作但没有产生输出的情况下提供错误
You have fallen into a trap called automatic semicolon insertion . 你陷入了一个叫做自动分号插入的陷阱。 Specifically, there must not be a newline between
return
and {
具体来说,
return
和{
之间一定不能有换行符
var Tweet = Backbone.Model.extend({
defaults: function() {
return { // !!!
author: '',
status: ''
}; // as a convention, I add ; always here, though not strictly needed.
}
});
Otherwise Javascript considers this being 2 statements: 否则Javascript认为这是2个语句:
return;
which just returns the value undefined
, and 它只返回
undefined
的值,并且
{
author: '';
status: '';
}
which is a block compound statement, where there are 2 labels : author:
and status:
, each followed by a no-op string literal expression statement ''
. 这是一个块复合语句,其中有2个标签 :
author:
和status:
,每个标签后跟一个no-op字符串文字表达式语句''
。 Adding a ,
after the author: ''
line makes it a syntax error, as a statement cannot end in a comma / the comma cannot be followed by a label. 添加
,
后author: ''
行使其成为一个语法错误,因为声明逗号不能结束/逗号不能跟一个标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.