这是我需要修改的前承包商编写的代码摘录。我迷失在 ({function (e)}) 和闭包中。
该代码向验证服务器发出 http 请求并返回一个令牌,该令牌设置了 myPlugin 对象的成员变量 (r = e)。然后,该函数将 foobar.js 动态加载到附加到 head 标记的新脚本标记中。foobar.js 中的代码引用了 myPlugin 对象的 getter 函数。
我只需要删除 http 请求和回调并传递一个参数来设置 r 的值并将新的脚本标记附加到 head 标记。
每次我尝试修改 loadPluginJsFn 函数时都会出错。
谁能解释 loadPluginJsFn 函数在做什么以及“(myPlugin);”的目的是什么?在 foobar.js 的末尾和 myPlugin 对象末尾的 () ?
HTML文件
var myPlugin = (function() {
var t, n, r, o_value ;
return {
set somevalue(e) {
o_value = e;
},
get somevalue() {
return o_value;
},
init(config){
},
// This is where I don't understand what it happening
loadPluginJsFn: function(e) {
"function" == typeof e &&
e(function(e) {
e &&
((r = e),
(function(e) {
var t = document.createElement("script");
(t.type = "text/javascript"),
(t.src = e),
document.querySelector("head").appendChild(t);
})(
"js/foobar.js"
));
});
}
};
})();
myPlugin.loadPluginJsFn(function(callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
callback(xhr.responseText);
}
};
xhr.open("GET", "<some domain>", true);
});
foobar.js
(function (my_Plugin) {
const somevars = 'xxx';
var someMoreVars = '123'
function somefunctions(useDefaults = false) {
return somethingImportant;
}
})(myPlugin);
温温酱
梦里花落0921
相关分类