我有这段代码:
var Helper = function () {
this.prototype = {
loadScripts: function (scripts, path) {
scripts = $.map(scripts, function (scr) {
return $.getScript((path || '') + scr);
});
scripts.push($.Deferred(function (deferred) {
$(deferred.resolve);
}));
return $.when.apply($, scripts);
}
};
return {
loadScripts: loadScripts,
};
};
但我不能像Helper.loadScripts()一样访问方法loadScripts:
// Getting TypeError: FraggyHelper.loadScripts is not a function
Helper.loadScripts(['foo.js', 'bar.js'], '/path/to/');
为什么?我怎样才能访问这个功能?
答案 0 :(得分:1)
试试这个
var Helper = {
loadScripts: function (scripts, path) {
scripts = $.map(scripts, function (scr) {
return $.getScript((path || '') + scr);
});
scripts.push($.Deferred(function (deferred) {
$(deferred.resolve);
}));
return $.when.apply($, scripts);
}
};
答案 1 :(得分:0)
var Helper = (function () {
var loadScripts = function (scripts, path) {
scripts = $.map(scripts, function (scr) {
return $.getScript((path || '') + scr);
});
scripts.push($.Deferred(function (deferred) {
$(deferred.resolve);
}));
return $.when.apply($, scripts);
};
return {
loadScripts: loadScripts,
} })();
你可以像上面那样做IFFE
答案 2 :(得分:0)
将其更改为:
var Helper = function () {};
Helper.prototype.loadScripts = function (scripts, path) {
scripts = $.map(scripts, function (scr) {
return $.getScript((path || '') + scr);
});
scripts.push($.Deferred(function (deferred) {
$(deferred.resolve);
}));
return $.when.apply($, scripts);
};
// Getting TypeError: FraggyHelper.loadScripts is not a function
new Helper().loadScripts(['foo.js', 'bar.js'], '/path/to/');