var Tweet = Backbone.Model.extend({
defaults: function()
{
return
{
author: ''
status: ''
}
}
});
according to the syntax it should be
author: '',
status: ''
但这提供了一个错误,而代码中的那个工作但没有产生输出
最佳答案
您掉进了一个名为 automatic semicolon insertion 的陷阱.具体来说,在 return
和 {
var Tweet = Backbone.Model.extend({
defaults: function() {
return { // !!!
author: '',
status: ''
}; // as a convention, I add ; always here, though not strictly needed.
}
});
否则 Javascript 认为这是 2 条语句:
return;
它只返回值 undefined
,并且
{
author: '';
status: '';
}
这是一个 block 复合语句,其中有2个labels : author:
和 status:
,每个后跟一个无操作字符串文字表达式语句 ''
。在 author: ''
行之后添加 ,
会导致语法错误,因为语句不能以逗号结尾/逗号后面不能跟标签。
关于返回对象文字的 Javascript 语法问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23539772/