javascript 什么是 CommonJS,我为什么要关心它如何帮助编写 .js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15124451/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is CommonJS and why should I care how it can help in writing .js applications?
提问by Clay Nichols
I'm learning JS and someone more experienced mentioned that they use CommonJS to handle keeping js code organized (since there's no "module" functionality).
我正在学习 JS 并且更有经验的人提到他们使用 CommonJS 来处理保持 js 代码的组织(因为没有“模块”功能)。
I looked on Quora, but it explained the groupand it's goals, not so much how I might use it.
我查看了 Quora,但它解释了该组及其目标,而不是我如何使用它。
采纳答案by RayViljoen
CommonJS modules enables you to include javascript modules within the current scope and effectively keeps the global scope from being polluted.
CommonJS 模块使您能够在当前范围内包含 javascript 模块,并有效地防止全局范围被污染。
This massively reduces the chance of naming collisions and keeps code organised.
这大大减少了命名冲突的机会并保持代码组织。
That said I think it's pretty safe to say that AMD modules have now become more popular than CommonJS.
也就是说,我认为 AMD 模块现在比 CommonJS 更受欢迎是非常有把握的。
The RequireJS website has a very good section on why to use modulesand the differences between CommonJS and AMD.
RequireJS 网站有一个很好的部分,介绍了为什么要使用模块以及 CommonJS 和 AMD 之间的区别。