jQuery对象文字函数声明错误
jQuery Object literal function declaration error
我想知道这段代码有什么问题,它抛出一个语法错误:意外令牌" anotherfunction 如果我添加anotherFunction
部分。有人能解释一下为什么,JavaScript让我对所有这些不同的调用函数的方式感到困惑。
var ajax = {
parseJSONP : function(result) {
console.log(result)
//iterate each returned item
$.each(result.items, function(i, row) {
$('#listview_test').append('<li><a href="#headline"><img src="' + row.volumeInfo.imageLinks.thumbnail + '" class="ui-li-has-thumb"/><h3>' + row.volumeInfo.title + '</h3></a></li>');
});
//end iteration of data returned from server and append to the list
$('#listview_test').listview('refresh');
// refresh the list-view so new elements are added to the DOM
}
//error is here, I just wanted to add another function here to do
something
anotherFuction : function(){
}
}
}
您忘记了对象字面量中第一个成员的关闭}
之后的,
。
您需要分隔每个成员,如下所示:
var ajax = {
parseJSONP : function(result) {
$.each(result.items, function(i, row) {
$('#listview_test').append('<li><a href="#headline"><img src="' + row.volumeInfo.imageLinks.thumbnail + '" class="ui-li-has-thumb"/><h3>' + row.volumeInfo.title + '</h3></a></li>');
});
$('#listview_test').listview('refresh');
},
// ^ Here
anotherFuction : function(){
// No more syntax errors!
}
}
当你使用"对象字面语法"时,例如:
var object = {};
您必须用逗号分隔所有"成员"(变量,函数)(,
)。
那么你的代码将变成:
var ajax = {
parseJSONP: function(result) { /* function body from above */ },
something,
anotherFunction: function() {}
}
注意在something
和parseJSONP
之后添加了逗号(,
)
相关文章:
- 错误:'让'声明只能在块内部声明
- DOJO 引用错误:未定义声明
- 将“*”分配给作用域中声明的变量的角度错误
- Dojo 声明出现意外的标识符错误
- AngularJS:当我已经声明$scope时,获取$scope不是定义的错误
- Javascript函数声明野牛语法减少/减少错误
- 在 AngularJS 中声明一个承诺,并带有命名的成功/错误回调
- 错误:“缺少;在声明之前”
- 从外部文件在打字稿中声明变量得到引用错误:..未定义
- “函数声明不能嵌套在语句中” - IE 处于严格模式错误
- 获取 IE 和 Firefox 错误:解析值“xxx”时出错.声明被丢弃
- 咖啡脚本变量声明放在错误的位置
- (教程)脚本由于语法错误而无法运行:块范围的声明
- JSLint 错误:未声明的“图像”、未声明的“文档”、超出范围
- 引用错误,用于使用 let 检查延迟声明的变量
- 在使用 Gulp 在 javascript 中声明对象时,在 Git bash 中出现以下错误“语法错误:意外标识符”
- JSON在IE8中没有声明错误
- jQuery对象文字函数声明错误
- JavaScript函数声明错误
- 套接字.io + Express: Express声明错误