post 重复参数
Oftentimes we override or functions and, in many cases, there are arguments we don't care too much about. A common practice for those arguments is using _
for argument names -- it's a generally accepted and known practice for "this isn't important". I started thinking about multiple useless arguments and if you could use the same name for the sake of minification -- you can.
通常,我们会覆盖或函数,并且在许多情况下,有些参数我们不太在意。 这些参数的常见做法是使用_
作为参数名称-这是“这并不重要”的普遍接受的已知做法。 我开始考虑多个无用的参数,如果为了简化而可以使用相同的名称,则可以。
So what happens when you use the same argument name more than once? An error? Uses the first value? The last value? Let's have a look:
那么当您多次使用相同的参数名称时会发生什么呢? 一个错误? 使用第一个值? 最后的价值? 我们来看一下:
function myFunc(_, _, _) { console.log("_: ", _);}myFunc(1, 2, 3);// >> 3
The duplicated argument is given the value of the last provided argument. If, however, "use strict"
is used, an error will be thrown.
为重复的参数提供最后提供的参数的值。 但是,如果"use strict"
,将引发错误。
For some reason I expected an error when using an argument name more than once. On the other end, you can change argument values so I shouldn't be surprised. Anyway, happy coding!
由于某种原因,我多次使用参数名称时会出现错误。 另一方面,您可以更改参数值,这样我就不会感到惊讶。 无论如何,祝您编程愉快!
翻译自:
post 重复参数