ES6 对象字面量增强写法

1,088 阅读1分钟

ES6 中,对对象字面量进行了很多增强。

  1. 属性初始化简写
// ES6 之前
let name = 'yang';
let age = 25;
let obj1 = {
    name:name,
    age:age
}
//ES6 写法
let obj2 = {
    name,age
}
  1. 方法的简写
// ES6 之前
let obj1 = {
    test:function(){
        console.log("Hello World")
    }
}
//ES6 写法
let obj2 = {
    test(){
        console.log("Hello World")
    }
}