将 JavaScript 命名空间拆分为多个文件

发布于 2024-10-19 13:19:35 字数 233 浏览 7 评论 0原文

假设我有一个这样的命名空间:

var myNamespace = {
    foo: function() {
    },
    bar: function() {
    }
};

将此代码拆分为分别定义 foobar 的文件的最佳方法是什么?

我不担心加载时间 - 我会在部署之前将其连接回一个文件。

Let's say I have a namespace like that:

var myNamespace = {
    foo: function() {
    },
    bar: function() {
    }
};

What is the best way to split this code into files defining foo and bar separately?

I'm not worried about loading time - I'll concatenate it back into one file before deployment.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感受沵的脚步 2024-10-26 13:19:35

在每个文件的开头:

if(myNameSpace === undefined) {
  var myNameSpace = {};
}

文件 1:

myNamespace.foo = function()...

文件 2:

myNamespace.bar = function()...

At the start of each file:

if(myNameSpace === undefined) {
  var myNameSpace = {};
}

File 1:

myNamespace.foo = function()...

File 2:

myNamespace.bar = function()...
凉风有信 2024-10-26 13:19:35
// File1:
// top level namespace here:
var myNamespace = myNamespace || {};

// File2:
myNamespace.foo = function() {
    // some code here...
}
// File1:
// top level namespace here:
var myNamespace = myNamespace || {};

// File2:
myNamespace.foo = function() {
    // some code here...
}
喵星人汪星人 2024-10-26 13:19:35

在每个文件中遵循以下模式:

(function(nameSpace) {
    nameSpace.foo = function() { ... };
})(window.nameSpace = window.nameSpace || {});

这样加载顺序并不重要。

In each file follow this pattern:

(function(nameSpace) {
    nameSpace.foo = function() { ... };
})(window.nameSpace = window.nameSpace || {});

This way load ordering is unimportant.

流云如水 2024-10-26 13:19:35

在单独的文件中进行简单定义,如下所示:

文件 1:

var myNamspace = {};

文件 2:

myNamespace.foo = function()...

文件 3:

myNamespace.boo = function()...

只要确保以正确的顺序加载文件即可。

Simple define in seperate files like this:

File 1:

var myNamspace = {};

File 2:

myNamespace.foo = function()...

File 3:

myNamespace.boo = function()...

Just make sure you load the files in the right order.

滥情哥ㄟ 2024-10-26 13:19:35
(function (NS) {
    NS.Uber = function Uber() {
        this.super = new NS.Super(); // yes, it works!
    }; //
}(NS = NS || {}));

// ------------- other file -----------------

(function (NS) {
    NS.Super = function Super() {
        this.uber = new NS.Uber(); // yes, it will also work!
    }; //
}(NS = NS || {}));

// -------------- application code ------------

var uber = new NS.Uber();
console.log(uber.super);

var super = new NS.Super();
console.log(super.uber);
(function (NS) {
    NS.Uber = function Uber() {
        this.super = new NS.Super(); // yes, it works!
    }; //
}(NS = NS || {}));

// ------------- other file -----------------

(function (NS) {
    NS.Super = function Super() {
        this.uber = new NS.Uber(); // yes, it will also work!
    }; //
}(NS = NS || {}));

// -------------- application code ------------

var uber = new NS.Uber();
console.log(uber.super);

var super = new NS.Super();
console.log(super.uber);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文