Javascript 中的错误 shorthand 属性 初始值设定项无效

Error Invalid shorthand property initializer in Javascript

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 属性 initializer 错误?

你可以做到这一点。

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

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

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