es6中的默认值不适用于箭头功能

时间:2017-05-02 04:21:47

标签: javascript ecmascript-6 babeljs

handleSuccessFeatureListing = (selectedOption=7) => {
    console.log(selectedOption);
}

为什么selectedOption仍然可以为null?我以为我已经将7设置为selectedOption的参数的默认值?

1 个答案:

答案 0 :(得分:8)

只有在没有参数或undefined值的情况下调用函数时,默认值才会生效。如果使用handleSuccessFeatureListing调用null,则会传递null

e.g。

function fn(arg = 7){
  return arg;
}

fn() === 7
fn(undefined) === 7
fn(6) === 6
fn(null) === null

因此,如果您收到null,那么是因为null在您预期undefined值时传递给该函数。

相关问题