如何在 Javascript 中声明可选的函数参数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12797118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I declare optional function parameters in Javascript?
提问by Uttam Dutta
Can I declare default parameter like
我可以声明默认参数吗
function myFunc( a, b=0)
{
// b is my optional parameter
}
in javascript.
在 JavaScript 中。
回答by Tigraine
With ES6:This is now part of the language:
使用 ES6:这现在是语言的一部分:
function myFunc(a, b = 0) {
// function body
}
Please keep in mind that ES6 checks the values against undefined
and not against truthy-ness (so only real undefined values get the default value - falsy values like null will not default).
请记住,ES6 根据undefined
而不是根据真实性检查值(因此只有真正的未定义值才会获得默认值 - 像 null 这样的虚假值不会默认)。
With ES5:
使用 ES5:
function myFunc(a,b) {
b = b || 0;
// b will be set either to b or to 0.
}
This works as long as all values you explicitly pass in are truthy.
Values that are not truthy as per MiniGod's comment: null, undefined, 0, false, ''
只要您明确传入的所有值都是true,这就会起作用。根据 MiniGod 的评论,不真实的值:null, undefined, 0, false, ''
It's pretty common to see JavaScript libraries to do a bunch of checks on optional inputs before the function actually starts.
看到 JavaScript 库在函数实际启动之前对可选输入进行大量检查是很常见的。
回答by Ja?ck
Update
更新
With ES6, this is possible in exactly the manner you have described; a detailed description can be found in the documentation.
使用 ES6,这完全可以按照您所描述的方式进行;详细说明可以在文档中找到。
Old answer
旧答案
Default parameters in JavaScript can be implemented in mainly two ways:
JavaScript 中的默认参数主要有两种实现方式:
function myfunc(a, b)
{
// use this if you specifically want to know if b was passed
if (b === undefined) {
// b was not passed
}
// use this if you know that a truthy value comparison will be enough
if (b) {
// b was passed and has truthy value
} else {
// b was not passed or has falsy value
}
// use this to set b to a default value (using truthy comparison)
b = b || "default value";
}
The expression b || "default value"
evaluates the value AND existence of b
and returns the value of "default value"
if b
either doesn't exist or is falsy.
该表达式b || "default value"
计算值 AND 的存在性b
,"default value"
如果b
不存在或为假则返回值。
Alternative declaration:
替代声明:
function myfunc(a)
{
var b;
// use this to determine whether b was passed or not
if (arguments.length == 1) {
// b was not passed
} else {
b = arguments[1]; // take second argument
}
}
The special "array" arguments
is available inside the function; it contains all the arguments, starting from index 0
to N - 1
(where N
is the number of arguments passed).
特殊的“数组”arguments
在函数内部可用;它包含所有参数,从索引开始0
到N - 1
(其中N
是传递的参数数量)。
This is typically used to support an unknown number of optional parameters (of the same type); however, stating the expected arguments is preferred!
这通常用于支持未知数量的可选参数(相同类型);但是,最好说明预期的参数!
Further considerations
进一步的考虑
Although undefined
is not writablesince ES5, some browsers are known to not enforce this. There are two alternatives you could use if you're worried about this:
虽然undefined
是不可写,因为ES5,一些浏览器被称为不执行此。如果您对此感到担心,可以使用两种替代方法:
b === void 0;
typeof b === 'undefined'; // also works for undeclared variables