请问有没有在JavaScript中跳过可选参数的好方法。
例如,我想要丢弃所有opt_
参数:
goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)
请问有没有在JavaScript中跳过可选参数的好方法。
例如,我想要丢弃所有opt_
参数:
goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)
解决方案:
goog.net.XhrIo.send(url, undefined, undefined, undefined, {'Cache-Control': 'no-cache'})
你应该使用undefined
来代替要跳过的可选参数,因为这样可以完全模拟JavaScript中可选参数的默认值。
下面是一个小例子:
myfunc(param);
//is equivalent to
myfunc(param, undefined, undefined, undefined);
强烈推荐: 如果你有很多参数,并且在参数列表中可以有可选参数,请使用JSON。看看jQuery是如何实现的。
最保险的选择是undefined
,几乎适用于所有情况。虽然如此,你无法欺骗函数被调用时认为你真正省略了一个参数。
如果你因为更短而倾向于使用null
,考虑声明一个名为_
的变量作为undefined
的缩写:
(function() { // First line of every script file
"use strict";
var _ = undefined; // For shorthand
// ...
aFunction(a, _, c);
// ...
})(); // Last line of every script
首先需要知道的是:
typeof undefined
的结果为 "undefined"
typeof null
的结果为 "object"
因此,假设一个函数需要一个值,并且期望它的类型为"number"
。如果你提供的值为 null
,那么实际上你传递给它的是一个"object"
类型,这会导致语义错误。1
随着开发人员不断编写更加强大的 JavaScript 代码,越来越多的函数显式地检查参数的值是否为 undefined
,而不是传统的 if (aParam) {...}
。如果你因为 null
和 undefined
都能强制转换为 false
而继续将它们视为可以互换使用的,那么你可能会遇到问题。
但要注意,事实上函数是可以判断一个参数是被省略了(未被传入)还是被设置为 undefined
的:
f(undefined); // Second param omitted
function f(a, b) {
// Both a and b will evaluate to undefined when used in an expression
console.log(a); // undefined
console.log(b); // undefined
// But...
console.log("0" in arguments); // true
console.log("1" in arguments); // false
}
脚注
undefined
虽然也不是 "number"
类型,但它的整个作用就是成为一种不真正存在的类型。这就是为什么它是未初始化变量的默认值,以及函数的默认返回值。function myfunc(x = 1, y = 2, z = 6) {
console.log(x);
console.log(y);
console.log(z);
}
myfunc(5) //Output: 5 2 6
myfunc(3, undefined, 8) //Output: 3 2 8
// Better way!
function myfunc(x = 1, y = 2, z = 6) {
console.log(x);
console.log(y);
console.log(z);
}
// skip y argument using: ...[,] and it's mean to undefine
// 1 argument for ...[,] 2 arguments for ...[,,] and so on.....
myfunc(7, ...[, ], 4); //Output: 7 2 4
...[,]
语法是什么神奇的东西?你介意分享一些文档吗? - Beki只需将参数值设置为null
。
补充说明:如果您想要传递实际值,则可以跳过最后一个可选参数之后的所有连续可选参数(在这种情况下,您可以完全跳过opt_timeoutInterval
参数)。
=== undefined
的检查吗? - Dangoog.net.XhrIo.send()
如何工作的帮助,请重新表述问题。 - nnnnnngoog.net.XhrIo.send
方法的开发人员如何检查可选参数。他可能会检查param === undefined
或typeof param === "undefined"
或param === null
甚至只是if(param)
。但是当您传递null值时,这意味着您意识到此参数是可选的,并明确告诉您不想传递任何有意义的值。 - Yuriy Rozhovetskiy== null
进行检查以允许undefined或null(除非由于某种原因null是合法值),但我可能会检查arguments
对象的长度,我也看到其他代码使用=== null
进行检查。 - nnnnnn
undefined
一样吗?),但它似乎最适合“跳过可选参数”。 - Thiloundefined
不能用于调用这些方法。需要引入一个if
语句来选择正确的参数列表长度wtf。 - Newlukai