Javascript jQuery对象文字函数声明错误
我想知道这段代码有什么问题,它抛出了一个语法错误:如果我添加Javascript jQuery对象文字函数声明错误,javascript,jquery,function,object,syntax-error,Javascript,Jquery,Function,Object,Syntax Error,我想知道这段代码有什么问题,它抛出了一个语法错误:如果我添加另一个函数部分,会出现意外的标记“另一个函数”。有人能解释一下为什么JavaScript会让我对调用函数的所有不同方式感到如此困惑吗 var ajax = { parseJSONP : function(result) { console.log(result) //iterate each returned item $.each(result
另一个函数
部分,会出现意外的标记“另一个函数”。有人能解释一下为什么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:函数(结果){
console.log(结果)
//迭代每个返回的项
$.each(result.items,函数(i,行){
$('listview_test')。追加('li>');
});
//结束从服务器返回的数据的迭代并附加到列表中
$('#listview_test')。listview('refresh');
//刷新列表视图,以便将新元素添加到DOM中
}
//错误在这里,我只是想在这里添加另一个函数
某物
另一个功能: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 ajax={
parseJSONP:函数(结果){
$.each(result.items,函数(i,行){
$('listview_test')。追加('li>');
});
$('#listview_test')。listview('refresh');
},
//^这里
另一个功能: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 ajax={
parseJSONP:函数(结果){
$.each(result.items,函数(i,行){
$('listview_test')。追加('li>');
});
$('#listview_test')。listview('refresh');
},
//^这里
另一个功能:function(){
//没有更多的语法错误!
}
}
当您使用“对象文字语法”时,例如:
必须用逗号(,
)分隔所有“成员”(变量、函数)
因此,您的代码将成为:
var ajax = {
parseJSONP: function(result) { /* function body from above */ },
something,
anotherFunction: function() {}
}
请注意,当您使用“对象文字语法”时,在something
和parseJSONP
之后添加了逗号(,
),例如:
必须用逗号(,
)分隔所有“成员”(变量、函数)
因此,您的代码将成为:
var ajax = {
parseJSONP: function(result) { /* function body from above */ },
something,
anotherFunction: function() {}
}
请注意,在
something
和parseJSONP
之后添加了逗号(,
),这有什么好处吗?或者为什么我们声明这样的函数?我不是指coma,而是在对象内部literals@DaimanLoks是的,但我不能像道格拉斯·克罗克福德那样解释它。请参阅调用部分(第4.3.1章)在他的书中,JavaScript——好的部分。你可以免费在线查看它——有什么好处或者为什么我们声明这样的函数吗?我不是指coma,而是指内部对象literals@DaimanLoks是的,但我不能像道格拉斯·克罗克福德那样解释它。请参阅调用部分(第4.3.1章)在他的书《JavaScript——好的部分》中,你可以免费在线观看-