ES6新特性之字面量增强写法

408 阅读1分钟

字面量增强的写法

ES6中对 对象字面量 进行了增强,称之为 Enhanced object literals(增强对象字面量)。字面量的增强主要包括以下几个部分:

  • 属性的简写:Property Shorthand
  • 方法的简写:Method Shorthand
  • 计算属性名:Computed Property Names
let name = "harry"
let age = 18

let obj = {
    name, //在vscode里画删除线,无需在意这是因为window对象中有name
    age,
    a:function(){

    },
    b(){

    },
    //箭头函数没有this
    arrow:()=>{
        console.log(this); //undefined
    },

    //computed property name (计算属性名)
    [name+123]:'哈哈哈'  //计算属性名格式: [表达式]:值
}

console.log(obj);