//ES5:
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());
最常见的简写是
name:function(){}
简写成
name(){}
长得像java中的函数了。