什么是“Splats”在CoffeeScript教程中的意思? [英] What does "Splats" mean in the CoffeeScript tutorial?

查看:317
本文介绍了什么是“Splats”在CoffeeScript教程中的意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看此CoffeeScript教程: http://jashkenas.github.com/coffee-script/ p>

我不太清楚Splats是什么。这是什么构造?它来自哪里(历史上)

解决方案

术语splat运算符来自Ruby,其中 * 字符(有时称为splat - 请参阅 Jargon文件条目)用于表示参数列表中的条目应该吸收参数列表。



CoffeeScript很早就采用了Ruby风格的splats请参阅问题16 ),但根据Douglas Crockford的建议,语法已从 * x x ... 几个星期后(请参阅问题45 )。不过,CoffeeScripers仍然将语法称为splat或splat运算符。

至于他们实际上做什么,splats切片 arguments 对象,使得splatted参数成为所有额外参数的数组。最小的例子是

 (args ...) - > 

在这种情况下, args arguments 的数组副本。 Splatted参数可以来自标准参数之前,之后或之间:

 (first,rest ...) 
(rest ...,last) - >
(first,rest ...,last) - >

在前两种情况下,如果函数接收到0-1个参数, rest 将是一个空数组。在最后一种情况下,该函数需要接收超过2个参数,用于 rest 为非空。



由于JavaScript不允许具有相同名称的函数的多个签名(C和Java的方式),因此splats是处理不同数量参数的巨大节省时间。


Looking at this CoffeeScript tutorial : http://jashkenas.github.com/coffee-script/

I don't quite see what the Splats is for. What is this construction? Where does it come from (historically)

解决方案

The term "splat operator" comes from Ruby, where the * character (sometimes called the "splat"—see the Jargon File entry) is used to indicate that an entry in an argument list should "soak up" a list of arguments.

CoffeeScript adopted Ruby-style splats very early on (see issue 16), but at Douglas Crockford's suggestion, the syntax was changed from *x to x... a couple of weeks later (see issue 45). Nevertheless, CoffeeScripters still refer to the syntax as the "splat" or "splat operator."

As to what they actually do, splats slice the arguments object in such a way that the splatted argument becomes an array of all "extra" arguments. The most trivial example is

(args...) ->

In this case, args will simply be an array copy of arguments. Splatted arguments can come either before, after, or between standard arguments:

(first, rest...) ->
(rest..., last) ->
(first, rest..., last) ->

In the first two cases, if the function receives 0-1 arguments, rest will be an empty array. In the last case, the function needs to receive more than 2 arguments for rest to be non-empty.

Since JavaScript doesn't allow multiple signatures for functions with the same name (the way C and Java do), splats are a huge time-saver for dealing with varying numbers of arguments.

这篇关于什么是“Splats”在CoffeeScript教程中的意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆