最近读了《CoffeeScript程序设计》的前半部分「核心 CoffeeScript」。对 CoffeeScript 也是有了初步的了解,本文只是个人随手笔记,并无很是系统的总结 CoffeeScript 语法,想学习语法的同窗能够看如下两份中文材料:javascript
CoffeeScript 移除了全部的大括号和分号。html
JS 会自动在行尾添加;
,但它又没有纯粹的设计为一款不须要加分号的语言,因此有时候会引发一些蛋疼的Bug。而 CoffeeScript 会在编译出的 JS 代码里每行都加;
,很方便。java
CoffeeScript 和 Python、Ruby 同样,采用强制缩进(Coffee的不少地方与 ruby 相似),这种简洁,可读性又很强的代码,让人大爱。git
JS 中的变量做用域一直让人诟病。github
CoffeeScript 把编译生成的 JS 封装在一个匿名函数中:npm
(function(){ // 这里是编译生成的代码 }).call(this);
这样就巧妙避免了全局做用域的污染。同时,CoffeeScript 始终在编译生成的 JS 代码中用 var
声明变量。数组
CoffeeScript 中有个操做符 ?
,用于检测变量是否存在。ruby
console.log html if html?
这句 CoffeeScript 编译过来为(去掉了匿名封装函数,为了方便,以后的编译后代码都去掉)less
if (typeof html !== "undefined" && html !== null) { console.log(html); }
可见,?
会先检测变量有没有定义,若是定义了再检测是否为 null。ide
CoffeeScript 中去掉了 function
关键字。用 () ->
定义一个函数。括号内为参数,能够为参数设置默认值。如:
myFunction = (a, b = 2) -> a + b
编译为:
var myFunction; myFunction = function(a, b) { if (b == null) { b = 2; } return a + b; };
调用函数的时候,还能够不用括号。如:
myFunction 3, 5
有一点须要注意一下,CoffeeScript 会在编译后的 JS 代码中自动为最后一行添加 return
关键字。因此不论函数的最后一行是什么,都会成为返回值。若是你不想让最后一行成为返回值,就须要另起一行本身加上 return
。
splat 操做符很是强大。在你的函数须要接受可变数量的参数时就须要它了。书上的栗子:
splatter = (etc...) -> console.log "Length: #{etc.length}, Values: #{etc.join(', ')}" // CoffeeScript 中字符串插值用 #{} splatter() splatter("a", "b", "c") // 输出 Length: 0, Values: Length: 3, Values: a, b, c
就在某个参数后面加上...
,就使传入的参数自动转化为一个数组。splat 操做符能够出如今参数列表的任意位置,可是参数列表中只能有一个 splat 操做符。
通常定义数组是这样:
myArray = ["a", "b", "c"]
在 CoffeeScript 里你还能够这样:
myArray = [ "a" "b" "c" ]
Array.prototype.indexOf
,在 CoffeeScript 中只须要用 in
:console.log "d was not be found" unless "d" in myArray // 输出 d was not be found
x = "X" y = "Y" [x, y] = [y, x]
交换 x、y 的值就这么简单!
myArray = ["A", "B", "C", "D"] [start, middle..., end] = myArray // 可配合 splat 操做符使用 console.log "start: #{start}" console.log "middle: #{middle}" console.log "end: #{end}" // 输出 start: A middle: B,C end: D
区间能让定义包含两个数字之间全部数字的数组变得很容易。
myRange = [1..10] console.log myRange // 输出 [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
若是不想包括结束数值,能够用 ...
代替 ..
。
myRange = [10...1] console.log myRange // 输出 [ 10, 9, 8, 7, 6, 5, 4, 3, 2 ]
常见的数组操做,均可以经过区间完成:
myArray = [1..10] // 分割数组 part = myArray[0..2] console.log part // 输出 [ 1, 2, 3 ] // 替换数组值 myArray = [1..10] myArray[4..7] = ["a", "b", "c", "d"] console.log myArray // 输出 [ 1, 2, 3, 4, 'a', 'b', 'c', 'd', 9, 10 ] // 插入值 myArray = [1..10] myArray[4..-1] = ["a", "b", "c", "d"] console.log myArray // 输出 [ 1, 2, 3, 4, 'a', 'b', 'c', 'd', 5, 6, 7, 8, 9, 10 ]
一例胜千言:
class Animal constructor: (@name) -> move: (meters) -> alert @name + " moved #{meters}m." class Snake extends Animal move: -> alert "Slithering..." super 5 class Horse extends Animal move: -> alert "Galloping..." super 45 sam = new Snake "Sammy the Python" tom = new Horse "Tommy the Palomino" sam.move() tom.move()
这是官网的例子,麻麻不再用担忧我在 JS 里使用类和继承了 T_T。
new
的时候调用,能够重写它。::
就和 JS 里的 prototype
同样我对「语法糖」的理解就是让代码的读写更简单。
CoffeeScript 中添加了一些关键字,如 unless when then until do
等。不只如此,CoffeeScript 引入了不少别名来代替一些关键字:
别名 | 对应关键字 |
---|---|
is | === |
isnt | !== |
not | ! |
and | && |
or | || |
true, yes, on | true |
false, no, off | false |
@, this | this |
of | in |
in | no JS equivalent |
运用别名和新关键字,使代码读起来就和普通的英文同样。并且 CoffeeScript 还自动为你添加关键字,如函数最后的 return
,switch
后自动添加 break
(这种符合咱们惰性的改进都是伟大的!ヽ( ^∀^)ノ)。
1.安装:
npm install -g coffee-script
2.用法:
安装完成后,直接在命令行中输入 coffee
,就进入了 CoffeeScript 的 REPL(Read-eval-print-loop) 模式,这是一个可交互的控制台,你能够输入 CoffeeScript 代码当即执行。如图:
也能够用指令编译 CoffeeScript 代码执行(CoffeeScript 代码文件后缀名为 coffee):
coffee -c hello_world.coffee
还有一些其余选项,我目前用的最多的是 -o
、-p
、-w
这三个。
-o
即 --output
,设置编译后 JS 文件输出到指定文件夹-p
即 --print
,直接在终端打印出编译后的 JS 代码-w
即 --watch
,监视文件改变,一有变化就从新执行这条指令
coffee -c hello_world.coffee -o ./js -w
搭配起来就能够边写边编译到指定文件夹。
有篇文章《CoffeeScript: The beautiful way to write JavaScript》,对 JS 和 CoffeeScript 的论述很中肯。但文中对「什么才是优美的代码」的总结更让人印象深入:
- beautiful code uses the least amount of code to solve a given problem
- beautiful code is readable and understandable
- beautiful code is achieved not when there is nothing more to add, but when there is nothing left to take away (just like great designs)
- the minimal length is a side-effect of beautiful code and not a goal or a measure
原文连接:CoffeeScript 笔记