0

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?

4

2 回答 2

1

不要在对象文字中使用 '=' 。它应该是raichu : {

let samson_pokemon = {
    raichu : {
        species: 'Rare pokemon',
        height: 0.8,
        weight: 12
    }
}
于 2021-06-19T07:19:25.083 回答
0

你可以这样做。

let weight4 = (typeof samson_pokemon !== 'undefined') ? samson_pokemon.pikachu : samson_pokemon.weight;
于 2021-06-19T07:16:23.367 回答