LearningRecord icon indicating copy to clipboard operation
LearningRecord copied to clipboard

默认参数如何工作?

Open Rashomon511 opened this issue 5 years ago • 0 comments

1.基本用法

function first(x = 1, y = 2) {
    console.log("x:"+x ,"y:"+ y);
}
first();
first(100);

2.与解构赋值默认值结合

function second({x, y = 2}) {
    console.log("x:"+x ,"y:"+ y);
}
second({});
second({x:100});
second({x:100,y:200})

3.双重默认值

function third({x = 1 ,y = 2} = {}) {
    console.log("x:"+x ,"y:"+ y);
}
third();
third({x:100,y:200});
third({x:100});

Rashomon511 avatar Jul 16 '19 03:07 Rashomon511