coffeeScript

CoffeeScript is a small language that compiles to JavaScript.  Under the clunky appearance of Java, JavaScript actually has a gorgeous heart. CoffeeScript tries to show the good parts of JavaScript in a concise way.

The guiding principle of CoffeeScript is:  "It's just JavaScript" . Code is compiled to JS one-to-one, not interpreted during compilation. Existing JavaScript libraries can be used seamlessly with CoffeeScript and vice versa. The compiled code is readable and beautified to run in all JavaScript environments, and should be as fast as or faster than its handwritten counterpart.

Latest version:  1.7.1

sudo npm install -g coffee-script

Overview

On the left is CoffeeScript, on the right is the compiled JavaScript output.

# 赋值:
number   = 42
opposite = true

# 条件:
number = -42 if opposite

# 函数:
square = (x) -> x * x

# 数组:
list = [1, 2, 3, 4, 5]

# 对象:
math =
  root:   Math.sqrt
  square: square
  cube:   (x) -> x * square x

# Splats:
race = (winner, runners...) ->
  print winner, runners

# 存在性:
alert "I knew it!" if elvis?

# 数组 推导(comprehensions):
cubes = (math.cube num for num in list)
var cubes, list, math, num, number, opposite, race, square,
  __slice = [].slice;

number = 42;

opposite = true;

if (opposite) {
  number = -42;
}

square = function(x) {
  return x * x;
};

list = [1, 2, 3, 4, 5];

math = {
  root: Math.sqrt,
  square: square,
  cube: function(x) {
    return x * square(x);
  }
};

race = function() {
  var runners, winner;
  winner = arguments[0], runners = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
  return print(winner, runners);
};

if (typeof elvis !== "undefined" && elvis !== null) {
  alert("I knew it!");
}

cubes = (function() {
  var _i, _len, _results;
  _results = [];
  for (_i = 0, _len = list.length; _i < _len; _i++) {
    num = list[_i];
    _results.push(math.cube(num));
  }
  return _results;
})();
run: cubes

Install

The CoffeeScript compiler itself is written in CoffeeScript and uses the  Jison parser generator . The command-line version of  coffee  is a useful  Node.js  tool. However , the compiler does not depend on Node, but can run in any JavaScript execution environment, such as in In your browser (see "Try CoffeeScript" above).

安装前你需要最新稳定版 Node.js, 和 npm (Node Package Manager). 借助 npm 可以安装 CoffeeScript:

npm install -g coffee-script

(如果不想全局安装可以去掉 -g 选项.)

如果你希望安装 master 分支上最新的 CoffeeScript, 你可以从源码仓库 克隆 CoffeeScript, 或直接下载源码. 还有通过 npm 方式安装 master 分支最新的 CoffeeScript 编译器:

npm install -g http://github.com/jashkenas/coffee-script/tarball/master

或者你想将其安装到 /usr/local, 而不用 npm 进行管理, 进入 coffee-script 目录执行:

sudo bin/cake install

用法

安装之后, 你应该可以运行 coffee 命令以执行脚本, 编译 .coffee 文件到 .js 文件, 和提供一个交互式的 REPL. coffee 命令有下列参数:

-c, --compile 编译一个 .coffee 脚本到一个同名的 .js 文件.
-m, --map 随 JavaScript 文件一起生成 source maps. 并且在 JavaScript 里加上 sourceMappingURL 指令.
-i, --interactive 启动一个交互式的 CoffeeScript 会话用来尝试一些代码片段. 等同于执行 coffee 而不加参数.
-o, --output [DIR] 将所有编译后的 JavaScript 文件写到指定文件夹. 与 --compile 或 --watch 搭配使用.
-j, --join [FILE] 编译之前, 按参数传入顺序连接所有脚本到一起, 编译后写到指定的文件. 对于编译大型项目有用.
-w, --watch 监视文件改变, 任何文件更新时重新执行命令.
-p, --print JavaScript 直接打印到 stdout 而不是写到一个文件.
-s, --stdio 将 CoffeeScript 传递到 STDIN 后从 STDOUT 获取 JavaScript. 对其他语言写的进程有好处. 比如:
cat src/cake.coffee | coffee -sc
-l, --literate 将代码作为 Literate CoffeeScript 解析. 只会在从 stdio 直接传入代码或者处理某些没有后缀的文件名需要写明这点.
-e, --eval 直接从命令行编译和打印一小段 CoffeeScript. 比如:
coffee -e "console.log num for num in [10..1]"
-b, --bare 编译到 JavaScript 时去掉顶层函数的包裹.
-t, --tokens 不对 CoffeeScript 进行解析, 仅仅进行 lex, 打印出 token stream:[IDENTIFIER square] [ASSIGN =] [PARAM_START (] ...
-n, --nodes 不对 CoffeeScript 进行编译, 仅仅 lex 和解析, 打印 parse tree:
Expressions
  Assign
    Value "square"
    Code "x"
      Op *
        Value "x"
        Value "x"
--nodejs node 命令有一些实用的参数, 比如
--debug--debug-brk--max-stack-size, 和 --expose-gc. 用这个参数直接把参数转发到 Node.js. 重复使用 --nodejs 来传递多个参数.

来源链接:http://coffee-script.org

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326906461&siteId=291194637