ES6-对象的简写方式

    var name = 'tom';
    var age = 11;
    //es5定义对象
    var obj = {
        name:name,
        age:age,
        getName:function(){
            return this.name;
        }
    };
    //es6定义对象可以简写
    var obj1={
        name,//当对象属性名可外面的变量名一致时候可以这样简写
        age,
        getName(){//定义方法时可以省略掉function关键字
            return this.name
        }
    };
    console.log(obj.getName());
    console.log(obj1.getName());

猜你喜欢

转载自www.cnblogs.com/maycpou/p/12330624.html