在MDN上,不同的括号可选参数符号是什么意思 [英] What do different bracket optional argument notations mean on MDN
问题描述
我正在为我的应用程序编写js文档,所以我正在使用jsDoc约定,并引用MDN作为指导和bla bla bla,但我的问题是如何在MDN引用上编写文档。
采取两种简单的方法:
console.log()
和数学.min()
及其在MDN上记录的语法
console.log(obj1 [,obj2, ...,objN]);
console.log(msg [,subst1,...,substN]);
Math.min([value1 [,value2 [,...]]])
如果我正确理解 console.log
说,我需要第一个参数,其余的是可选的。可选参数用括号+逗号 [,
表示,而必需的参数只用逗号,
。
此外,为了引用很多参数,文档写成 subst1,...,substN
查看 Math.min
这是非常相似和明显不同的,但为什么?我理解 Math.min中的第一个参数Math.min
是可选的(返回 Infinity
),而在console.log中,它不是可选的(你会得到 undefined
)。如果我理解语法正确, Math.min
的语法可以写成,
Math.min([value1,...,valueN])
参数是可选的,该方法接受零个或多个参数,但是我们在每个参数附近都有括号。
有没有理由说明文档的写法
其实我总是相信语法 fn( a,[b,c,d])
意味着您只能使用 fn(a)
或 fn a,b,c,d)
。
但无论如何, console.log
和 Math.min
的行为方式完全相同 - 它们处理所有参数。如果您为任一功能提供零参数,则不会出现错误。这是我在控制台中得到的:
> Math.min()
无限
> console.log()
(无输出)
另外,返回值 console.log
独立于参数 - 始终未定义
:
console.log()== console.log(666)// true
This is very trivial, but I like to know why things are the way they are. I'm doing my best at writing js documentation for my apps and so I am using the jsDoc conventions and referring to MDN as a guide and bla bla bla but my question regards how the documentation is written on the MDN reference.
Take two simple methods:
console.log()
and Math.min()
and their syntax documented on MDN
console.log(obj1 [, obj2, ..., objN]);
console.log(msg [, subst1, ..., substN]);
Math.min([value1[, value2[, ...]]])
If I understand correctly console.log
says, I require the first argument and the rest are optional. The optional arguments are indicated with a bracket + comma "[,
" whereas a required argument would be indicated with just a comma ",
".
In addition, to refer to many arguments, the documentation is written as, "subst1, ..., substN
".
Looking at Math.min
this is very similar and obviously different, but why? I understand the first arg in Math.min
is optional (returns Infinity
) and in console.log it is not optional (you would get undefined
). If I understand the syntax correctly the syntax for Math.min
could be written as,
Math.min([value1, ..., valueN])
stating that ALL arguments are optional and that the method accepts zero or many arguments, but instead we get brackets around every argument.
Is there a reason for the difference in how the documentation is written and if so please explain what that difference is.
Actually I was always convinced that syntax fn(a, [b, c, d])
means that you can only use either fn(a)
or fn(a, b, c, d)
.
But anyway, console.log
and Math.min
behave exactly the same way - they process all the arguments. There is no error if you provide zero arguments to either function. This is what I get in the console:
> Math.min()
Infinity
> console.log()
(nothing to output)
Also, return value of console.log
is independent on the parameters - always undefined
:
console.log()==console.log(666) //true
这篇关于在MDN上,不同的括号可选参数符号是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!