namespace("M.n.p", function (self, $) {
self.doSomethingElse = function () {
$("p").text("something else");
};
}, jQuery);
You can use augmentation to separate your modules out in to different files. Once you are ready for a production release, you can concatenate those files into one or several files.
File1 defines module M.n
var M = M || {};
M.n = M.n || {};
(function (self) {
self.doSomething = function () {
console.log("something");
};
})(M.n);
File2 defines module M.n.p
var M = M || {};
M.n = M.n || {};
M.n.p = M.n.p || {};
(function (self) {
self.doSomethingElse = function () {
console.log("something else");
};
})(M.n.p);
Now in your "main" script you can freely use the members of these modules.
M.n.doSomething();
M.n.p.doSomethingElse();
Defining the modules can be a little tedious, but you should be able to whip something up to automate it. In the past, I've used this little script to help make it easier, but feel free to make your own. You may even be able to bake in dependency management with consistent file naming.
var namespace = function(path, context, args) {
var finalLink = namespace._generateChain(path, window);
context.apply(finalLink, [finalLink].concat(args));
};
namespace._generateChain = function(path, root) {
var segments = path.split('.'),
cursor = root,
segment;
for (var i = 0; i < segments.length; ++i) {
segment = segments[i];
cursor = cursor[segment] = cursor[segment] || {};
}
return cursor;
};
To use:
namespace("M.n.p", function (self) {
self.doSomethingElse = function () {
console.log("something else");
};
});
If for some reason you want to include a variable under a different alias, you can pass it to the namespace function and it will be passed to the function as an argument.
namespace("M.n.p", function (self, $) {
self.doSomethingElse = function () {
$("p").text("something else");
};
}, jQuery);
Use RequireJS to organize things. For shared logic, the shared methods must be stored on a globally accessible namespace, or accessed via require(). I didn't like having to make many require() calls for the application code, so I include modules in chunks and each attach to a particular namespace via define inclusion.
发布评论
评论(2)
您可以使用扩充将模块分离到不同的文件中。一旦准备好发布生产版本,您就可以将这些文件连接成一个或多个文件。
File1 定义模块 Mn
File2 定义模块 Mnp
现在,在您的“主”脚本中,您可以自由地使用这些模块的成员。
定义模块可能有点乏味,但您应该能够快速实现自动化。过去,我使用这个小脚本来帮助简化操作,但您可以随意制作自己的脚本。您甚至可以通过一致的文件命名来进行依赖管理。
使用方法:
如果由于某种原因你想在不同的别名下包含一个变量,你可以将它传递给命名空间函数,它将作为参数传递给函数。
You can use augmentation to separate your modules out in to different files. Once you are ready for a production release, you can concatenate those files into one or several files.
File1 defines module M.n
File2 defines module M.n.p
Now in your "main" script you can freely use the members of these modules.
Defining the modules can be a little tedious, but you should be able to whip something up to automate it. In the past, I've used this little script to help make it easier, but feel free to make your own. You may even be able to bake in dependency management with consistent file naming.
To use:
If for some reason you want to include a variable under a different alias, you can pass it to the namespace function and it will be passed to the function as an argument.
使用 RequireJS 来组织事物。对于共享逻辑,共享方法必须存储在全局可访问的命名空间中,或通过 require() 访问。我不喜欢对应用程序代码进行多次 require() 调用,因此我将模块包含在块中,并通过定义包含将每个模块附加到特定的命名空间。
Use RequireJS to organize things. For shared logic, the shared methods must be stored on a globally accessible namespace, or accessed via require(). I didn't like having to make many require() calls for the application code, so I include modules in chunks and each attach to a particular namespace via define inclusion.