【问题标题】:Best practice for splitting up a large js script into smaller scripts?将大型 js 脚本拆分为较小脚本的最佳实践?
【发布时间】:2023-01-27 16:37:11
【问题描述】:

我有一个包含大约 2.000 行代码的脚本,这变得太难以概览了。

我想将它拆分成更小的 js 文件,然后将它们捆绑到一个文件中(以避免往返服务器)。

现在我需要了解如何在运行时将所有这些脚本“合并”到一个文件中。我查看了“模块”,但看起来好像他们必须访问网络服务器(是很多往返)。

到目前为止,这是我的想法:

// myUi.js
var myUi = new (function () {
  this.InitUi = function () {
    console.log("Init UI");
  };
})();

// myData.js
var myData = new (function () {
  this.InitData = function () {
    console.log("Init Data");
  };
})();

// myEvents.js
var myEvents = new (function () {
  this.InitEvents = function () {
    console.log("Init Events");
  };
})();

var func = $.extend({}, myUi, myData, myEvents);

func.InitUi();
func.InitData();
func.InitEvents();

但是有最佳实践吗?你做什么工作?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    $是什么意思。在你的代码中?是jquery吗?

    【讨论】:

      猜你喜欢
      • 2021-12-01
      • 2015-11-17
      • 2017-06-11
      • 2021-01-24
      • 2021-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      相关资源
      最近更新 更多