[英]jQuery Object literal function declaration error
I am wondering what is wrong with this code, it throws a Syntax Error: unexpected token "anotherFuction if I add the anotherFunction
part. Can someone please explain why, JavaScript is confusing me so much with all this different ways calling functions. 我想知道这段代码有什么问题,如果我添加
anotherFunction
部分,它将引发语法错误:意外令牌“ anotherFuction 。有人可以解释一下原因,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(){
}
}
}
You're forgetting the ,
after the closing }
of the first member in the object literal. 你忘了
,
闭幕后}
第一构件对象中的文字。
You need to separate each member, as shown below: 您需要分离每个成员,如下所示:
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!
}
}
When you use the "Object Literal Syntax", eg: 当使用“对象文字语法”时,例如:
var object = {};
You have to separate all "members" (variables, functions) with a comma ( ,
). 您必须用逗号(
,
)分隔所有“成员”(变量,函数)。
So your code will become: 因此,您的代码将变为:
var ajax = {
parseJSONP: function(result) { /* function body from above */ },
something,
anotherFunction: function() {}
}
notice the addition of the commas ( ,
) after something
and parseJSONP
. 注意添加逗号(的
,
)之后something
和parseJSONP
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.