JavaScript 模块模式/组织/子模块
- 我想知道是什么 不同之处 (优点/缺点)之间 以下模式。
- 如何创建基于子模块 关于模块模式?
我的目标是将我的 js 组织成多个文件,这些文件是延迟加载但具有一个命名空间。
例如:
SO.global(global.js) SO.global.registration (registration.js) <- 加载
var SO = function(){
var CONSTANT = 'Z';
function createX(){
alert("create X");
}
function getY(){
alert("get Y");
}
return{
create:createX,
get:getY
}
}();
//SO.createX();
//SO.getY();
VS.
var SO = (function() {
var CONSTANT = 'Z';
function createX(){
alert("create X");
}
function getY(){
alert("get Y");
}
return {
create:createX,
get:getY
}
} ());
- I would like to know what's the
difference
(advantages/disadvantages) between
the following patterns. - How can I create sub modules based
on the module pattern?
My goal is to have my js organized into multiple files that are lazy loaded but have one namespace.
For example:
SO.global (global.js)
SO.global.registration (registration.js) <- load
var SO = function(){
var CONSTANT = 'Z';
function createX(){
alert("create X");
}
function getY(){
alert("get Y");
}
return{
create:createX,
get:getY
}
}();
//SO.createX();
//SO.getY();
VS.
var SO = (function() {
var CONSTANT = 'Z';
function createX(){
alert("create X");
}
function getY(){
alert("get Y");
}
return {
create:createX,
get:getY
}
} ());
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否考虑过Require.JS?它尝试提供以下解决方案:
Require.JS 实现 模块/异步定义 由 Common.JS 定义的规范
Have you considered Require.JS? It attempts to provide the following solution:
Require.JS implements the Module/Asynchronous Definition spec defined by Common.JS
这是一个很好的读物: http://snook.ca/archives/javascript/ no-love-for-module-pattern,另一个:http://lamb.cc /blog/category/javascript/
YUI 热衷于使用它,我也是如此,我还没有发现任何受到它限制的情况,并且它与自定义模块的 YUI 依赖加载器很好地集成。
(抱歉,我知道这不是完整的答案,但有一些未经篡改的信息供您使用)
Here's a good read: http://snook.ca/archives/javascript/no-love-for-module-pattern, and another: http://lamb.cc/blog/category/javascript/
YUI uses it avidly, as do I, I haven't found any situations where I was restricted by it, and it nicely integrates with the YUI dependency loader for custom Modules.
(Sorry, I realise this isn't a complete answer, but there's some untampered info for you)