将 JavaScript 命名空间拆分为多个文件
假设我有一个这样的命名空间:
var myNamespace = {
foo: function() {
},
bar: function() {
}
};
将此代码拆分为分别定义 foo
和 bar
的文件的最佳方法是什么?
我不担心加载时间 - 我会在部署之前将其连接回一个文件。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在每个文件的开头:
文件 1:
文件 2:
At the start of each file:
File 1:
File 2:
在每个文件中遵循以下模式:
这样加载顺序并不重要。
In each file follow this pattern:
This way load ordering is unimportant.
在单独的文件中进行简单定义,如下所示:
文件 1:
文件 2:
文件 3:
只要确保以正确的顺序加载文件即可。
Simple define in seperate files like this:
File 1:
File 2:
File 3:
Just make sure you load the files in the right order.