Javascript 重构Crossrider扩展代码
我使用构建扩展,目前所有代码都在extension.js文件中。然而,它变得相当长,并且在这个单一文件中维护变得更加困难。有没有办法将我的代码分割成不同的文件,并在扩展名中使用它们 例如,如果我的extension.js文件的结构与下面的示例类似,我希望函数f1和f2位于一个单独的文件中,我可以将其加载到扩展中:Javascript 重构Crossrider扩展代码,javascript,cross-browser,include,browser-extension,crossrider,Javascript,Cross Browser,Include,Browser Extension,Crossrider,我使用构建扩展,目前所有代码都在extension.js文件中。然而,它变得相当长,并且在这个单一文件中维护变得更加困难。有没有办法将我的代码分割成不同的文件,并在扩展名中使用它们 例如,如果我的extension.js文件的结构与下面的示例类似,我希望函数f1和f2位于一个单独的文件中,我可以将其加载到扩展中: appAPI.ready(function($) { init(); f1(); f2(); ... function init() { // init
appAPI.ready(function($) {
init();
f1();
f2();
...
function init() {
// init code
...
}
function f1() {
//hundreds of lines of code
}
function f2() {
//hundreds of lines of code
}
...
});
您可以在一个或多个资源文件(例如init.js、functions.js)中定义函数,然后将其包含在其中。有关资源的更多信息,请参阅 因此,使用您的示例,您的代码如下所示: extension.js:
appAPI.ready(function($) {
appAPI.resources.includeJS('init.js');
appAPI.resources.includeJS('functions.js');
init();
f1();
f2();
...
});
function init() {
// init code
...
}
function f1() {
//hundreds of lines of code
}
function f2() {
//hundreds of lines of code
}
init.js:
appAPI.ready(function($) {
appAPI.resources.includeJS('init.js');
appAPI.resources.includeJS('functions.js');
init();
f1();
f2();
...
});
function init() {
// init code
...
}
function f1() {
//hundreds of lines of code
}
function f2() {
//hundreds of lines of code
}
functions.js:
appAPI.ready(function($) {
appAPI.resources.includeJS('init.js');
appAPI.resources.includeJS('functions.js');
init();
f1();
f2();
...
});
function init() {
// init code
...
}
function f1() {
//hundreds of lines of code
}
function f2() {
//hundreds of lines of code
}
[披露:我是一名交叉骑手员工]