javaScript function - why my default argument fails?

前端 未结 3 2464
离开以前
离开以前 2021-02-20 08:20

My Javascript function leads my console to return me :

TypeError: style is null

Here the snippet:

3条回答
  •  难免孤独
    2021-02-20 09:05

    Rename your style variable into styles and then instead of having null as your first argument when you invoke styling, use undefined:

    const styles = {
      one: 1,
      two: 2,
      three: 3
    }
    
    function styling(style = styles, ...ruleSetStock) {
    
      return ruleSetStock.map(ruleSet => {
        console.log(ruleSet)
        return style[ruleSet]
      })
    }
    
    console.log(styling(undefined, "one", "two", "three"))
    // one
    // two
    // three
    // [1, 2, 3]
    

提交回复
热议问题