gpt4 book ai didi

返回对象文字的 Javascript 语法问题

转载 作者:行者123 更新时间:2023-11-30 12:43:24 24 4
gpt4 key购买 nike

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com