我有一个 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);
}
我的问题是如何将刚刚加载的脚本返回的公共对象作为参数传递给回调?因为我希望尽快使用这些公共方法......</p>