- splats比较特别, 我在php里没有遇到过类似的语法. 主要有两方面
- 定义函数时, 在最后一个参数上使用splats, 表示把函数调用时这个参数及后续参数作为一个数组赋给该参数
- 在使用一个数组作为参数传给调用的函数时, 在数组后面加上splats, 则会把数组拆开传给调用的函数
例如这段coffeescript代码:
splatter = (one, two, three...) -> console.log "1: #{one}" console.log "2: #{two}" console.log "3: #{three.join(',')}" splatter 1, 2, 3, 4, 5 splatter [1..5]...
生成的javascript代码如下:
var splatter, __slice = [].slice; splatter = function() { var one, three, two; one = arguments[0], two = arguments[1], three = 3 <= arguments.length ? __slice.call(arguments, 2) : []; console.log("1: " + one); console.log("2: " + two); return console.log("3: " + (three.join(','))); }; splatter(1, 2, 3, 4, 5); splatter.apply(null, [1, 2, 3, 4, 5]);