2017-09-06 51 views
0

我只是想知道为什么在初始化下面给出的函数的参数之后给出了一个“= {}”,这是一个空的默认值,任何人都可以回答这个问题吗?为什么react-redux connect()给出这样的默认值?

return function connect(
mapStateToProps, 
mapDispatchToProps, 
mergeProps, 
{ 
    pure = true, 
    areStatesEqual = strictEqual, 
    areOwnPropsEqual = shallowEqual, 
    areStatePropsEqual = shallowEqual, 
    areMergedPropsEqual = shallowEqual, 
    ...extraOptions 
} = {} ){ // code is here } 

回答

0

您可以设置对象的参数和解构结构属性的默认值。 ={}是您想要解构的函数参数的默认值。如果你没有定义默认值,并且该方法的调用者将不会提供一个对象,调用将失败,错误:

const demo = ({ a = 1, b = 2 }) => ({ a, b }); 
 

 
console.log(demo({})); // works fine because it tries to destructure an object 
 

 
console.log(demo()); // fails because the destructuring target is undefined

如果您设置的默认值={}它可以处理不定值,以及:

const demo = ({ a = 1, b = 2 } = {}) => ({ a, b }); 
 

 
console.log(demo()); // works because it has a default value

+0

感谢大利德罗尔我,你解决了我的问题。 – Jackman

+0

@杰克曼 - 欢迎:) –

相关问题