【问题标题】:Error Invalid shorthand property initializer in Javascript [closed]Javascript中的错误速记属性初始化程序无效[关闭]
【发布时间】:2023-03-24 17:09:01
【问题描述】:

let will_pokemon = {
    pikachu: {
        species: 'Mouse pokemon',
        height: 0.4,
        weight: 5
    }
}

let samson_pokemon = {
    raichu = {
        species: 'Rare pokemon',
        height: 0.8,
        weight: 12
    }
}

let weight4 = samson_pokemon?.pikachu?.weight //check either object property available if not will be undefined

console.log(weight4)

为什么在我的 Chrome 浏览器上运行时出现 Invalid shorthand property initializer 错误?

【问题讨论】:

  • 修复:raichu: {
  • 在 Chrome 和 Firefox 上运行良好。

标签: javascript ecmascript-2020


【解决方案1】:

您不要在 object literals 中使用“=”。应该是raichu : {

let samson_pokemon = {
    raichu : {
        species: 'Rare pokemon',
        height: 0.8,
        weight: 12
    }
}

【讨论】:

    【解决方案2】:

    你可以这样做。

    let weight4 = (typeof samson_pokemon !== 'undefined') ? samson_pokemon.pikachu : samson_pokemon.weight;
    

    【讨论】:

      猜你喜欢
      • 2017-06-19
      • 1970-01-01
      • 1970-01-01
      • 2012-12-15
      • 1970-01-01
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多