对象字面量的简化,推荐使用
发布于:2022-01-10 09:41:42
次阅读
// y 对象字面量的简化,推荐使用// !属性简化let name = "辛";let user = { // name: "xin", name, // * 1 变量name 与 属性 name同名 * // * 2 且再同一个作用域中, 可以不写变量名};console.log(user.name);// ! 方法简化let user1 = { // 方法仍然是属性,只不过它的值是一个函数声明 name, getName: function() { return this.name; }, // 简化:将“:function”删除 getFirstname() { return this.name + "test"; }, // 改成箭头函数,箭头函数不可以用在字面量中 // getLastname: () => this.name, // this:普通函数,调用时确定 // this:箭头函数,声明时确定,箭头函数中没有this这个变量};console.log(user1.getName());console.log(user1.getFirstname());// console.log(user1.getLastname());