CoffeeScript 模块的模式

2024-04-20前端开发问题
5

本文介绍了CoffeeScript 模块的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

在查看 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.jsbar.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 中的 xbar.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 模块的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

“数组中的每个孩子都应该有一个唯一的 key prop"仅在第一次呈现页面时
quot;Each child in an array should have a unique key propquot; only on first time render of page(“数组中的每个孩子都应该有一个唯一的 key prop仅在第一次呈现页面时)...
2024-04-20 前端开发问题
5