第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

javaScript 函數(shù) - 為什么我的默認參數(shù)失???

javaScript 函數(shù) - 為什么我的默認參數(shù)失???

千萬里不及你 2021-06-15 09:06:02
我的 Javascript 函數(shù)引導我的控制臺返回我:類型錯誤:樣式為空這里的片段:let style = {  one: 1,  two: 2,  three: 3}function styling(style = style, ...ruleSetStock) {  return ruleSetStock.map(ruleSet => {    console.log(ruleSet)    return style[ruleSet]  })}console.log(styling(null, "one", "two", "three"))我不明白為什么。在我看來一切都很棒任何提示都會很棒,謝謝。
查看完整描述

3 回答

?
慕容708150

TA貢獻1831條經(jīng)驗 獲得超4個贊

僅當no value或undefined被傳遞時才分配默認參數(shù)


let defaultStyle = {  one: 1, two: 2, three: 3 }


function styling(style = defaultStyle, ...ruleSetStock) {

  return ruleSetStock.map(ruleSet => {

    return style[ruleSet]

  })

}


console.log(styling(undefined, "one", "two", "three"))

如果我想在各種類型上使用默認值falsy values such as false, '', null 怎么辦?


您不能為此使用默認參數(shù),但可以使用 ||


let style1 = {  one: 1, two: 2, three: 3 }


function styling(style, ...ruleSetStock) {

  style = style || style1

  return ruleSetStock.map(ruleSet => {

    return style[ruleSet]

  })

}


console.log(styling(undefined, "one", "two", "three"))

console.log(styling(null, "one", "two", "three"))

console.log(styling('', "one", "two", "three"))

console.log(styling(0, "one", "two", "three"))


查看完整回答
反對 回復 2021-06-18
?
慕絲7291255

TA貢獻1859條經(jīng)驗 獲得超6個贊

你需要更新的兩件事

  1. 傳遞默認參數(shù)沒有值或未定義

  2. 將樣式默認變量更改為另一個名稱

請查看更新后的代碼

let defaultStyle = {

  one: 1,

  two: 2,

  three: 3

}


function styling(style = defaultStyle, ...ruleSetStock) {


  return ruleSetStock.map(ruleSet => {

    console.log(ruleSet)

    return style[ruleSet]

  })

}


console.log(styling(undefined, "one", "two", "three"))


您可以使用 es6 以更簡潔的方式編寫上述代碼段


見下面的片段


const defaultStyle = {

  one: 1,

  two: 2,

  three: 3

}



const styling = (style = defaultStyle, ...ruleSetStock) => ruleSetStock.map(ruleSet => {

   return style[ruleSet]

})


console.log(styling(undefined, "one", "two", "three"))


查看完整回答
反對 回復 2021-06-18
?
繁花如伊

TA貢獻2012條經(jīng)驗 獲得超12個贊

將style變量重命名為styles,然后null在調(diào)用時將其作為第一個參數(shù),而不要styling使用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]


查看完整回答
反對 回復 2021-06-18
  • 3 回答
  • 0 關注
  • 198 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號