es6函数默认值

身边的同事都太卷了,大晚上的在群里说tag函数,🙂没有听过

熟悉了下常用的函数默认值,发现了两个新东西 😂

1.如果函数有默认值,参数是不可以同名的,同名会报错,脑残会这么传参吧😂

如下

// 不报错
function foo(x, x, y) {
  // ...
}

// 报错
function foo(x, x, y = 1) {
  // ...
}
// SyntaxError: Duplicate parameter name not allowed in this context

2.默认值不传或者传undefined触发,null默认值 不会触发

function foo(x = 5, y = 6) {
  console.log(x, y);
}

foo(undefined, null)
// 5 null

  

function foo(x = 5, y = 6) {
  console.log(x, y);
}

foo(undefined, null)
// 5 null
posted @ 2022-03-03 22:34  小僵尸  阅读(70)  评论(0编辑  收藏  举报