JavaScript 函数中的默认参数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6486307/
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
Default argument values in JavaScript functions
提问by Douglas Crockford
Possible Duplicate:
How do I make a default value for a parameter to a javascript function
in PHP:
在 PHP 中:
function func($a = 10, $b = 20){
// if func() is called with no arguments $a will be 10 and $ b will be 20
}
How can you do this in JavaScript?
你怎么能在 JavaScript 中做到这一点?
I get a error if I try to assign values in function arguments
如果我尝试在函数参数中分配值,我会收到错误消息
missing ) after formal parameters
缺少 ) 在形式参数之后
回答by Ravan Scafi
In javascript you can call a function (even if it has parameters) without parameters.
在 javascript 中,你可以调用一个没有参数的函数(即使它有参数)。
So you can add default values like this:
所以你可以添加这样的默认值:
function func(a, b){
if (typeof(a)==='undefined') a = 10;
if (typeof(b)==='undefined') b = 20;
//your code
}
and then you can call it like func();to use default parameters.
然后你可以像func();使用默认参数一样调用它。
Here's a test:
这是一个测试:
function func(a, b){
if (typeof(a)==='undefined') a = 10;
if (typeof(b)==='undefined') b = 20;
alert("A: "+a+"\nB: "+b);
}
//testing
func();
func(80);
func(100,200);
回答by Boopathi Rajaa
ES2015 onwards:
ES2015 起:
From ES6/ES2015, we have default parameters in the language specification. So we can just do something simple like,
从 ES6/ES2015 开始,我们在语言规范中有默认参数。所以我们可以做一些简单的事情,比如,
function A(a, b = 4, c = 5) {
}
or combined with ES2015 destructuring,
或者结合ES2015 解构,
function B({c} = {c: 2}, [d, e] = [3, 4]) {
}
For detailed explanation,
如需详细说明,
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters
Default function parameters allow formal parameters to be initialized with default values if no valueor undefinedis passed.
如果未传递值或未定义,则默认函数参数允许使用默认值初始化形式参数。
Pre ES2015:
ES2015 前:
If you're going to handle values which are NOTNumbers, Strings, Boolean, NaN, or nullyou can simply use
如果您要处理不是数字、字符串、布尔值、 的值NaN,或者null您可以简单地使用
(So, for Objects, Arrays and Functions that you plan never to send null, you can use)
(因此,对于您计划永远不会发送的对象、数组和函数null,您可以使用)
param || DEFAULT_VALUE
for example,
例如,
function X(a) {
a = a || function() {};
}
Though this looks simple and kinda works, this is restrictive and can be an anti-pattern because ||operates on all falsy values ("", null, NaN, false, 0) too - which makes this method impossible to assign a param the falsy value passed as the argument.
虽然这看起来很简单而且有点工作,但这是限制性的并且可能是一种反模式,因为它也||对所有假值("", null, NaN, false, 0)进行操作 - 这使得该方法无法将作为参数传递的假值分配给参数。
So, in order to handle only undefinedvalues explicitly, the preferred approach would be,
因此,为了仅undefined显式处理值,首选方法是,
function C(a, b) {
a = typeof a === 'undefined' ? DEFAULT_VALUE_A : a;
b = typeof b === 'undefined' ? DEFAULT_VALUE_B : b;
}
回答by jtbandes
回答by tkahn
I have never seen it done that way in JavaScript. If you want a function with optional parameters that get assigned default values if the parameters are omitted, here's a way to do it:
我从未见过在 JavaScript 中这样做过。如果您想要一个带有可选参数的函数,如果省略参数,则为其分配默认值,这是一种方法:
function(a, b) {
if (typeof a == "undefined") {
a = 10;
}
if (typeof b == "undefined") {
a = 20;
}
alert("a: " + a + " b: " + b);
}
回答by Gabriele Petrioli
function func(a, b)
{
if (typeof a == 'undefined')
a = 10;
if (typeof b == 'undefined')
b = 20;
// do what you want ... for example
alert(a + ',' + b);
}
in shorthand
简写
function func(a, b)
{
a = (typeof a == 'undefined')?10:a;
b = (typeof b == 'undefined')?20:b;
// do what you want ... for example
alert(a + ',' + b);
}
回答by evilone
You cannot add default values for function parameters. But you can do this:
您不能为函数参数添加默认值。但是你可以这样做:
function tester(paramA, paramB){
if (typeof paramA == "undefined"){
paramA = defaultValue;
}
if (typeof paramB == "undefined"){
paramB = defaultValue;
}
}

