简体   繁体   English

为什么Javascript docs将函数的参数定义为语法中的嵌套数组?

[英]Why Javascript docs define function's parameter as a nested array in syntax?

I don't know how to ask this question, but while i'm studying documentation of Javascript or Node.js they are defining syntax something like this. 我不知道如何提出这个问题,但在我学习Javascript或Node.js的文档时,他们正在定义类似这样的语法。

var new_array = old_array.concat(value1[, value2[, ...[, valueN]]]) var new_array = old_array.concat(value1 [,value2 [,... [,valueN]]])

here all the parameters( value1[, value2[, ...[, valueN]]] ) are defined as a nested array. 这里所有参数( value1[, value2[, ...[, valueN]]] )都被定义为嵌套数组。

what actually it means, and 实际意味着什么,和

why not they are just defining something like this. 为什么他们不只是定义这样的东西。

var new_array = old_array.concat(...values) var new_array = old_array.concat(... values)

OR 要么

var new_array = old_array.concat(valueList) var new_array = old_array.concat(valueList)

It has nothing to do with arrays (except that your example is taken from the documentation of a method on the array type). 它与数组无关(除了你的例子来自数组类型的方法的文档)。 The square bracket means an optional argument and this is a convention used in most programming languages and tools. 方括号表示可选参数 ,这是大多数编程语言和工具中使用的约定。 For example, the help for the ls command: 例如, ls命令的帮助:

usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

... or cp : ......或者cp

usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file target_file
       cp [-R [-H | -L | -P]] [-fi | -n] [-apvXc] source_file ... target_directory

Your example means that the function takes at least one argument and more arguments can be added separated by a comma. 您的示例意味着该函数至少使用一个参数,并且可以使用逗号分隔更多参数。 According to the docs these values can be Arrays and/or values to concatenate into a new array . 根据文档,这些值可以是数组和/或值以连接成新数组 So, each valueN can be an array or a value to add to the returned array. 因此,每个valueN可以是要添加到返回数组的数组或值。

The reason not to use the ... notation is probably because the square brackets have been used for decades and are widespread, so readers will be familiar with the syntax no matter what the subject is. 不使用...符号的原因可能是因为方括号已经使用了几十年并且很普遍,因此无论主题是什么,读者都会熟悉语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM