Javascript CoffeeScript 教程中的“Splats”是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6201657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:43:28  来源:igfitidea点击:

What does "Splats" mean in the CoffeeScript tutorial?

javascriptcoffeescript

提问by interstar

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

看看这个 CoffeeScript 教程: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)

我不太明白 Splats 是做什么用的。这是什么建筑?它来自哪里(历史上)

回答by Trevor Burnham

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.

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

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

CoffeeScript中采用红宝石风格的泼溅(见很早问题16),但道格拉斯Crockford的建议下,语法是从改变*xx...几个星期之后(见第45期)。尽管如此,CoffeeScripters 仍然将语法称为“splat”或“splat operator”。

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

至于他们实际做什么,argumentssplats 以这样一种方式对对象进行切片,即 splatted 参数成为所有“额外”参数的数组。最简单的例子是

(args...) ->

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

在这种情况下,args将只是arguments. Splatted 参数可以出现在标准参数之前、之后或之间:

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

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

在前两种情况下,如果函数接收 0-1 个参数,rest将是一个空数组。在最后一种情况下,该函数需要接收 2 个以上的参数rest才能为非空。

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.

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

回答by keppla

if you know python, args...roughly similar to *args, as it allows you to treat function parameters as list

如果您了解 python,args...大致类似于*args,因为它允许您将函数参数视为列表

for example:

例如:

concat = (args...) -> args.join(', ')
concat('hello', 'world') == 'hello, world'
concat('ready', 'set', 'go!') == 'ready, set, go!'

it works in assginments, too:

它也适用于分配:

[first, rest...] = [1, 2, 3, 4]
first == 1
rest == [2, 3, 4]

回答by Jeff Foster

Splats is the term for the use of the ...operator for var-args (functions that take a variable number of arguments).

Splats 是使用...var-args(采用可变数量参数的函数)运算符的术语。

回答by Zifei Tong

I think it is a syntactic sugar for javascript's arguments object.

我认为它是 javascript 参数对象的语法糖。

The idea may come form ruby's splat operator *.

这个想法可能来自 ruby​​ 的splat operator*