写对象时,属性名和变量名相同可以只写一个,方法可以省略 function 和冒号。
| 以前 | 现在 |
|---|---|
let name = "大喇叭";
let age = 24;
let obj = {
name: name,
age: age
};
|
let name = "大喇叭";
let age = 24;
let obj = {
name, // 属性名和变量名相同,只写一个
age
};
|
| 以前 | 现在 |
|---|---|
let obj = {
say: function () {
console.log("我会演小品");
}
};
obj.say();
|
let obj = {
say() { // 省略 function 和冒号
console.log("我会演小品");
}
};
obj.say();
|
| 以前 | 现在 |
|---|---|
let school = "大喇叭";
let change = function () {
console.log("改变世界");
};
let obj = {
school: school,
change: change,
say: function () {
console.log("言行一致");
}
};
|
let school = "大喇叭";
let change = function () {
console.log("改变世界");
};
let obj = {
school,
change,
say() {
console.log("言行一致");
}
};
|