简体   繁体   English

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

[英]default value in es6 doesn't work with arrow function

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

Why selectedOption still can be null? 为什么selectedOption仍然可以为null? I thought I already set 7 as the default value for the param of selectedOption? 我以为我已经将7设置为selectedOption的参数的默认值?

Default values only come into affect if the function is called with no argument, or with an undefined value. 只有在没有参数或undefined值的情况下调用函数时,默认值才会生效。 If handleSuccessFeatureListing is called with null , null will be passed through. 如果使用null调用handleSuccessFeatureListing ,则将传递null

eg 例如

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

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

so if you are getting a null , then it is because null is being passed to the function when you expected an undefined value. 所以如果你得到一个null ,那么因为当你期望一个undefined值时, null被传递给该函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM