Meaning of variable in array as property name?

前端 未结 3 654
醉梦人生
醉梦人生 2021-01-29 12:00
var expertise = \'journalism\'
var person = {
    name: \'Sharon\',
    age: 27,
    [expertise]: {
        years: 5,
        interests: [\'international\', \'politics\'         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-29 12:54

    It is Computed property (ES2015) :

    This is a sample:

    var prop = 'foo';
    var o = {
      [prop]: 'hey',
      ['b' + 'ar']: 'there'
    };
    

    and you may use :

    console.log(o.foo)  // hey
    console.log(o.bar)  // there
    

提交回复
热议问题