简写有两条基本原则:this
来看下下面这个例子,我分别用ES5 和 ES6 的语法分别定义并声明了一个简单的学生对象:spa
ES5:code
var studentES5 = { name: '小方哥', age: 20, sex: '男', getName: function () { return this.name; } } console.log('ES5', studentES5); console.log('ES5', studentES5.getName());
ES6:对象
const name = 'Jack'; const age = 25; const sex = '女'; const studentES6 = { name,// 同名的属性能够省略不写 age, sex, getName() {// 能够省略方法中的 : function return this.name; } }; console.log('ES6', studentES6); console.log('ES6', studentES6.getName());