在 JavaScript 中跳过可选函数参数
您能给我指出在 JavaScript 中跳过可选参数的好方法吗?
例如,我想在这里丢弃所有 opt_
参数:
goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)
Could you please point me to the nice way of skipping optional parameters in JavaScript.
For example, I want to throw away all opt_
parameters here:
goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
解决方案:
您应该使用
undefined
而不是要跳过的可选参数,因为这 100% 模拟了 JavaScript 中可选参数的默认值。小例子:
强烈推荐:如果参数很多,请使用 JSON,并且参数列表中间可以有可选参数。看看 jQuery 中是如何完成此操作的。
Solution:
You should use
undefined
instead of optional parameter you want to skip, because this 100% simulates the default value for optional parameters in JavaScript.Small example:
Strong recommendation: use JSON if you have a lot of parameters, and you can have optional parameters in the middle of the parameters list. Look how this is done in jQuery.
简短回答
最安全的选择是
未定义
,并且应该几乎无处不在。但最终,您无法欺骗被调用的函数,使其认为您确实省略了参数。如果您发现自己倾向于使用
null
只是因为它更短,请考虑声明一个名为_
的变量作为undefined
的一个很好的简写:详细信息
首先,知道:
typeof undefined
计算结果为"undefined"
typeof null
计算结果为"object"
所以假设一个函数采用它期望的类型的参数
“数字”
。如果您提供null
作为值,则您将为其提供一个“object”
。语义已关闭。1随着开发人员继续编写越来越健壮的 JavaScript 代码,您调用的函数显式检查参数值是否为
undefined
的可能性越来越大,而不是经典的if (aParam) {...}
。如果您继续将null
与undefined
互换使用,因为它们碰巧都强制转换为false
,那么您的处境就会岌岌可危。但请注意,函数实际上可以判断参数是否确实被省略(而不是设置为
undefined
):脚注
undefined
也不是"number"
类型,它的全部工作就是成为一种并非真正类型的类型。这就是为什么它是未初始化变量所采用的值以及函数的默认返回值。Short answer
The safest bet is
undefined
, and should work almost ubiquitously. Ultimately, though, you cannot trick the function being called into thinking you truly omitted a parameter.If you find yourself leaning towards using
null
just because it's shorter, consider declaring a variable named_
as a nice shorthand forundefined
:Details
First, know that:
typeof undefined
evaluates to"undefined"
typeof null
evaluates to"object"
So suppose a function takes an argument that it expects to be of type
"number"
. If you providenull
as a value, you're giving it an"object"
. The semantics are off.1As developers continue to write increasingly robust javascript code, there's an increasing chance that the functions you call explicitly check a parameter's value for
undefined
as opposed to the classicif (aParam) {...}
. You'll be on shaky ground if you continue to usenull
interchangeably withundefined
just because they both happen to coerce tofalse
.Be aware, though, that it is in fact possible for a function to tell if a parameter was actually omitted (versus being set to
undefined
):Footnotes
undefined
also isn't of type"number"
, it's whole job is to be a type that isn't really a type. That's why it's the value assumed by uninitialized variables, and the default return value for functions.通过使用 ES6 javascript!
By using ES6 javascript!
只需传递
null
作为参数值。添加:您还可以跳过最后一个要传递实际值的后续可选参数(在这种情况下,您可以完全跳过
opt_timeoutInterval
参数)Just pass
null
as parameter value.Added: you also can skip all consequent optional parameters after the last that you want to pass real value (in this case you may skip
opt_timeoutInterval
parameter at all)还有另一种选项可以避免在每个想要跳过的参数中写入特定的
未定义
值。在ES6
中,可以使用 使用数组扩展语法并将其用作函数调用的参数。如果您未指定值,则默认情况下它将被视为undefined
(请考虑到这可能比使用undefined
值的可读性较差)。出现这种情况是因为:
There is another option that avoids writing specific
undefined
values in every parameter that one wants to skip. InES6
one can make use of the Spread syntax with an array and use it as the parameters of the function call. If you don't specify a value, it will be taken asundefined
by default (take into account that this could be less readable than usingundefined
values).And this occurs because: