Skip to content

CoffeeScript

CoffeeScript 是一种编译成 JavaScript 的小语言。它提供了一个简单的语法,没有大量的括号和圆括号。代码一对一编译为等效的 JS,运行时无需解释。

¥CoffeeScript is a little language that compiles into JavaScript. It provides a simple syntax without lots of braces and parentheses. The code compiles one-to-one into the equivalent JS, and there is no interpretation at runtime.

客户端和服务器都支持 CoffeeScript。以 .coffee.litcoffee.coffee.md 结尾的文件会自动编译为 JavaScript。

¥CoffeeScript is supported on both the client and the server. Files ending with .coffee, .litcoffee, or .coffee.md are automatically compiled to JavaScript.

命名空间和 CoffeeScript

¥Namespacing and CoffeeScript

下面是 CoffeeScript 如何与 Meteor 的命名空间配合使用。

¥Here's how CoffeeScript works with Meteor's namespacing.

  • 按照通常的 CoffeeScript 约定,CoffeeScript 变量默认是文件范围的(仅在定义它们的 .coffee 文件中可见)。

    ¥Per the usual CoffeeScript convention, CoffeeScript variables are file-scoped by default (visible only in the .coffee file where they are defined.)

  • 编写包时,CoffeeScript 定义的变量可以像任何其他变量一样导出(参见 Package.js)。导出变量会将其拉到包范围,这意味着它将对你的应用或包中的所有代码可见(.js.coffee 文件)。

    ¥When writing a package, CoffeeScript-defined variables can be exported like any other variable (see Package.js). Exporting a variable pulls it up to package scope, meaning that it will be visible to all of the code in your app or package (both .js and .coffee files).

  • .js 文件中声明的包范围变量在同一应用或项目中的任何 .coffee 文件中可见。

    ¥Package-scope variables declared in .js files are visible in any .coffee files in the same app or project.

  • 除了导出它之外,没有其他方法可以从 .coffee 文件创建包范围变量。我们无法找到一种方法让它自然地融入 CoffeeScript 语言中。如果你想在 CoffeeScript 中使用包范围变量,一种方法是制作一个简短的 .js 文件,声明所有包范围变量。然后可以从 .coffee 文件使用和分配它们。

    ¥There is no way to make a package-scope variable from a .coffee file other than exporting it. We couldn't figure out a way to make this fit naturally inside the CoffeeScript language. If you want to use package-scope variables with CoffeeScript, one way is to make a short .js file that declares all of your package-scope variables. They can then be used and assigned to from .coffee files.

  • 如果你想在同一个包中的 .coffee 文件之间共享变量,并且不想在 .js 文件中单独声明它们,我们有一个你可能会喜欢的实验性功能。一个名为 share 的对象在 CoffeeScript 代码中可见,并在同一包中的所有 .coffee 文件中共享。因此,你可以将 share.foo 写为包中所有 CoffeeScript 代码之间共享的值,但不会脱离该包。

    ¥If you want to share variables between .coffee files in the same package, and don't want to separately declare them in a .js file, we have an experimental feature that you may like. An object called share is visible in CoffeeScript code and is shared across all .coffee files in the same package. So, you can write share.foo for a value that is shared between all CoffeeScript code in a package, but doesn't escape that package.

重度 CoffeeScript 用户,请让我们知道这种安排对你有何作用,share 是否对你有帮助,以及你希望看到的任何其他更改。

¥Heavy CoffeeScript users, please let us know how this arrangement works for you, whether share is helpful for you, and anything else you'd like to see changed.

模块和 CoffeeScript

¥Modules and CoffeeScript

参见 模块 » 语法 » CoffeeScript

¥See Modules » Syntax » CoffeeScript.