我希望JavaScript函数具有可选参数,我将其设置为默认值,如果未定义该值,则使用该参数。在Ruby中你可以这样做:
def read_file(file, delete_after = false)
# code
end
这适用于JavaScript吗?
function read_file(file, delete_after = false) {
// Code
}
我希望JavaScript函数具有可选参数,我将其设置为默认值,如果未定义该值,则使用该参数。在Ruby中你可以这样做:
def read_file(file, delete_after = false)
# code
end
这适用于JavaScript吗?
function read_file(file, delete_after = false) {
// Code
}
从ES6 / ES2015开始,默认参数在语言规范中。
function read_file(file, delete_after = false) {
// Code
}
只是工作。
参考: 默认参数 - MDN
默认函数参数允许使用默认值初始化形式参数if 没有价值 要么 未定义 通过。
你也可以 模拟默认值 命名 参数通过解构:
// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
// Use the variables `start`, `end` and `step` here
···
}
预ES2015,
有很多方法,但这是我首选的方法 - 它可以让你传递任何你想要的东西,包括false或null。 (typeof null == "object")
function foo(a, b) {
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
...
}
从ES6 / ES2015开始,默认参数在语言规范中。
function read_file(file, delete_after = false) {
// Code
}
只是工作。
参考: 默认参数 - MDN
默认函数参数允许使用默认值初始化形式参数if 没有价值 要么 未定义 通过。
你也可以 模拟默认值 命名 参数通过解构:
// the `= {}` below lets you call the function without any parameters
function myFor({ start = 5, end = 1, step = -1 } = {}) { // (A)
// Use the variables `start`, `end` and `step` here
···
}
预ES2015,
有很多方法,但这是我首选的方法 - 它可以让你传递任何你想要的东西,包括false或null。 (typeof null == "object")
function foo(a, b) {
a = typeof a !== 'undefined' ? a : 42;
b = typeof b !== 'undefined' ? b : 'default_b';
...
}
function read_file(file, delete_after) {
delete_after = delete_after || "my default here";
//rest of code
}
这是指定的 delete_after 的价值 delete_after 如果不是 falsey 值,否则它分配字符串 "my default here"。有关更多详细信息,请查看 Doug Crockford对该语言的调查,并查看运算符部分。
如果你想传入一个,这种方法不起作用 falsey 价值即 false, null, undefined, 0 要么 ""。如果您需要 falsey 要传递的值需要使用方法 汤姆里特的答案。
在处理函数的许多参数时,允许使用者在对象中传递参数参数然后通常是有用的 合并 这些值包含一个包含函数默认值的对象
function read_file(values) {
values = merge({
delete_after : "my default here"
}, values || {});
// rest of code
}
// simple implementation based on $.extend() from jQuery
function merge() {
var obj, name, copy,
target = arguments[0] || {},
i = 1,
length = arguments.length;
for (; i < length; i++) {
if ((obj = arguments[i]) != null) {
for (name in obj) {
copy = obj[name];
if (target === copy) {
continue;
}
else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
return target;
};
使用
// will use the default delete_after value
read_file({ file: "my file" });
// will override default delete_after value
read_file({ file: "my file", delete_after: "my value" });
我发现像这样简单的东西更加简洁和个性化。
function pick(arg, def) {
return (typeof arg == 'undefined' ? def : arg);
}
function myFunc(x) {
x = pick(x, 'my default');
}
在ECMAScript 6中,您实际上可以准确地写出您拥有的内容:
function read_file(file, delete_after = false) {
// Code
}
这将设定 delete_after 至 false 如果它不存在或 undefined。你今天可以使用像这样的ES6功能,例如 巴别塔。
默认参数值
使用ES6,你可以做一个最常见的习语 JavaScript 涉及为函数参数设置默认值。我们多年来这样做的方式看起来应该很熟悉:
function foo(x,y) {
x = x || 11;
y = y || 31;
console.log( x + y );
}
foo(); // 42
foo( 5, 6 ); // 11
foo( 5 ); // 36
foo( null, 6 ); // 17
这种模式最常用,但是当我们传递像这样的值时会很危险
foo(0, 42)
foo( 0, 42 ); // 53 <-- Oops, not 42
为什么?因为 0 is falsy等等 x || 11 results in 11,而不是直接传入0.为了解决这个问题,有些人会更加冗长地编写支票,如下所示:
function foo(x,y) {
x = (x !== undefined) ? x : 11;
y = (y !== undefined) ? y : 31;
console.log( x + y );
}
foo( 0, 42 ); // 42
foo( undefined, 6 ); // 17
我们现在可以检查添加的一个很好的有用语法 ES6 简化默认值到缺少参数的分配:
function foo(x = 11, y = 31) {
console.log( x + y );
}
foo(); // 42
foo( 5, 6 ); // 11
foo( 0, 42 ); // 42
foo( 5 ); // 36
foo( 5, undefined ); // 36 <-- `undefined` is missing
foo( 5, null ); // 5 <-- null coerces to `0`
foo( undefined, 6 ); // 17 <-- `undefined` is missing
foo( null, 6 ); // 6 <-- null coerces to `0`
x = 11 在函数声明中更像是 x !== undefined ? x : 11 而不是更常见的成语 x || 11
默认值表达式
Function 默认值可以不仅仅是简单的值,如31;它们可以是任何有效的表达,甚至是 function call:
function bar(val) {
console.log( "bar called!" );
return y + val;
}
function foo(x = y + 3, z = bar( x )) {
console.log( x, z );
}
var y = 5;
foo(); // "bar called"
// 8 13
foo( 10 ); // "bar called"
// 10 15
y = 6;
foo( undefined, 10 ); // 9 10
正如您所看到的,默认值表达式是延迟计算的,这意味着它们仅在需要时运行 - 也就是说,当参数的参数被省略或未定义时。
默认值表达式甚至可以是内联函数表达式调用 - 通常称为立即调用函数表达式 (IIFE):
function foo( x =
(function(v){ return v + 11; })( 31 )
) {
console.log( x );
}
foo(); // 42
该解决方案适用于js:
function read_file(file, delete_after) {
delete_after = delete_after || false;
// Code
}
只需使用与undefined的显式比较。
function read_file(file, delete_after)
{
if(delete_after === undefined) { delete_after = false; }
}