【发布时间】:2013-09-09 21:12:09
【问题描述】:
我正在编写一个全局 javascript 函数。在一些错误(以及这里的一些搜索)之后,我让它工作了。但我也看到了一个例子 (function($){ code here }(jQuery);
有什么区别(如果有的话),选项 1 和 2 之间有什么优势吗?两者都很好地完成了我的任务。我只是想了解其中的区别。
选项 #1
(function($){
TEAM={
getQB: function( success, failure) {
var user=USER.user_data.login.toUpperCase();
$.ajax({
type: "GET",
url: "/nfl/getQB?username="+user,
dataType: 'json',
async: false,
success: success,
error: failure,
timeout: 6000
});
},
getRB: function( success, failure )
{
userx=USER.user_data.login.toUpperCase();
$.ajax({
type: "GET",
url: "/nfl/getRB?username="+userx,
dataType: 'json',
async: false,
success: success,
error: failure,
timeout: 6000
});
}
}
})(jQuery);
选项 #2
var TEAM={
getQB: function( success, failure) {
var user=USER.user_data.login.toUpperCase();
$.ajax({
type: "GET",
url: "/nfl/getQB?username="+user,
dataType: 'json',
async: false,
success: success,
error: failure,
timeout: 6000
});
},
getRB: function( success, failure )
{
userx=USER.user_data.login.toUpperCase();
$.ajax({
type: "GET",
url: "/nfl/getRB?username="+userx,
dataType: 'json',
async: false,
success: success,
error: failure,
timeout: 6000
});
}
}
【问题讨论】:
-
如果这就是所有的代码,没有区别,除了选项#1在严格模式下会失败。
标签: javascript jquery ajax