问题描述
在查看 Github 上 CoffeeScript 的源代码时,我注意到大多数(如果不是全部)模块都是定义如下:
While reviewing the source code for CoffeeScript on Github, I noticed that most, if not all, of the modules are defined as follows:
(function() {
...
}).call(this);
这种模式看起来像是将整个模块包装在一个匿名函数中并调用自身.
This pattern looks like it wraps the entire module in an anonymous function and calls itself.
这种方法的优点(和缺点)是什么?还有其他方法可以实现相同的目标吗?
What are the pros (and cons) of this approach? Are there other ways to accomplish the same goals?
推荐答案
Harmen 的回答非常好,但让我详细说明一下 CoffeeScript 编译器在哪里以及为什么这样做.
Harmen's answer is quite good, but let me elaborate a bit on where this is done by the CoffeeScript compiler and why.
当你用 coffee -c foo.coffee
编译某些东西时,你总是会得到一个如下所示的 foo.js
:
When you compile something with coffee -c foo.coffee
, you will always get a foo.js
that looks like this:
(function() {
...
}).call(this);
这是为什么呢?好吧,假设你把一个任务像
Why is that? Well, suppose you put an assignment like
x = 'stringy string'
在 foo.coffee
中.当它看到时,编译器会问: x
是否已经存在于这个范围内,还是一个外部范围内?如果不是,它会在 JavaScript 输出中该范围的顶部放置一个 var x
声明.
in foo.coffee
. When it sees that, the compiler asks: Does x
already exist in this scope, or an outer scope? If not, it puts a var x
declaration at the top of that scope in the JavaScript output.
现在假设你写
x = 42
在 bar.coffee
中,编译两者,并将 foo.js
与 bar.js
连接以进行部署.你会得到
in bar.coffee
, compile both, and concatenate foo.js
with bar.js
for deployment. You'll get
(function() {
var x;
x = 'stringy string';
...
}).call(this);
(function() {
var x;
x = 42;
...
}).call(this);
所以 foo.coffee
中的 x
和 bar.coffee
中的 x
是完全隔离的其他.这是 CoffeeScript 的一个重要部分:变量永远不会从一个 .coffee 文件泄漏到另一个文件,除非显式导出(通过附加到共享的全局,或附加到 Node.js 中的 exports
)js).
So the x
in foo.coffee
and the x
in bar.coffee
are totally isolated from one another. This is an important part of CoffeeScript: Variables never leak from one .coffee file to another unless explicitly exported (by being attached to a shared global, or to exports
in Node.js).
您可以通过对 coffee
使用 -b
(bare")标志来覆盖它,但这应该只在非常特殊的情况下使用.如果你在上面的例子中使用它,你会得到的输出是
You can override this by using the -b
("bare") flag to coffee
, but this should only be used in very special cases. If you used it with the above example, the output you'd get would be
var x;
x = 'stringy string';
...
var x;
x = 42;
...
这可能会产生可怕的后果.要自己测试,请尝试在 foo.coffee
中添加 setTimeout (-> alert x), 1
.请注意,您不必自己连接两个 JS 文件 - 如果您使用两个单独的 <script>
标记将它们包含在页面上,它们仍然可以作为一个文件有效地运行.
This could have dire consequences. To test this yourself, try adding setTimeout (-> alert x), 1
in foo.coffee
. And note that you don't have to concatenate the two JS files yourself—if you use two separate <script>
tags to include them on a page, they still effectively run as one file.
通过隔离不同模块的作用域,CoffeeScript 编译器让您不必担心项目中的不同文件是否可能使用相同的局部变量名.这是 JavaScript 世界中的常见做法(例如,参见 jQuery 源代码,或任何 jQuery 插件)—CoffeeScript 会为您处理好它.
By isolating the scopes of different modules, the CoffeeScript compiler saves you from the headache of worrying whether different files in your project might use the same local variable names. This is common practice in the JavaScript world (see, for instance, the jQuery source, or just about any jQuery plugin)—CoffeeScript just takes care of it for you.
这篇关于CoffeeScript 模块的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!