我有一個JavaScript文件,它返回公共方法。這個JavaScript文件在開始時並未加載,只在需要時才進行預加載。例如:如何將返回的公共對象從加載的JavaScript文件傳遞到回調函數?
(function() {
// Some privat vars and functions
var something = 'Something goes well!';
function doSomething() {
console.log(something);
}
return {
doSomething: doSomething,
doAnotherThings: doAnotherThing
}
})();
我已經建立了jQuery的功能,預加載我的JavaScript文件時,我真的需要這麼做。這裏有一個函數:
function load(file, callback) {
var head = $("html").closest("head"),
tag = $("<script />", {
type: "text/javascript"
});
tag.on("load", callback);
tag.attr("src", file);
head.append(tag);
}
我的問題是我如何通過返回公共對象從剛剛加載腳本到回調作爲參數?因爲我希望儘快使用這些公共方法......