我希望JavaScript函数具有可选的参数,我在这些参数上设置了默认值,如果未定义值,则使用这些参数(如果传递了值,则忽略这些参数)。在Ruby中,您可以这样做:

def read_file(file, delete_after = false)
  # code
end

这在JavaScript中有效吗?

function read_file(file, delete_after = false) {
  // Code
}

当前回答

默认参数值

使用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是假的,所以x||11的结果是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!==未定义?x:11比更常见的成语x||11

默认值表达式

函数默认值可以不仅仅是像31这样的简单值;它们可以是任何有效的表达式,甚至是函数调用:

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

其他回答

export const getfilesize = (bytes, decimals = 2) => {
    if (bytes === 0){ 
        return '0 Bytes';
    }else{
        const k = 1024;
        const dm = decimals < 0 ? 0 : decimals;
        const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
        const i = Math.floor(Math.log(bytes) / Math.log(k));
        return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i];

    }
}

是-证明:

函数read_file(file,delete_after=false){//代码console.log({file,delete_after});}//测试读取文件(“A”);read_file(“B”,true);read_file(“C”,false);

只需使用未定义的显式比较。

function read_file(file, delete_after)
{
    if(delete_after === undefined) { delete_after = false; }
}

设置默认参数的另一种方法是使用参数的对象映射,而不是直接使用参数。例如

const defaultConfig = {
 category: 'Animals',
 legs: 4
};

function checkOrganism(props) {
 const category = props.category || defaultConfig.category;
 const legs = props.legs || defaultConfig.legs;
}

这样,很容易扩展参数,而不用担心参数长度不匹配。

默认参数值

使用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是假的,所以x||11的结果是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!==未定义?x:11比更常见的成语x||11

默认值表达式

函数默认值可以不仅仅是像31这样的简单值;它们可以是任何有效的表达式,甚至是函数调用:

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