ES2016
**
console.log(2 ** 3); // equal to Math.pow(2,3)
2. async function
code:javascript
async function sample() {
console.log("start");
await new Promise((s) => setTimeout(s, 3000));
console.log("3 seconds passed after start");
}
sample();
code:javascript
console.log("start");
await new Promise((s) => setTimeout(s, 3000));
console.log("3 seconds passed after start");
2**53 - 1
Number.MAX_SAFE_INTEGER
BigInt可以表現超越此值的數值
於數值後方加上n即可
const maxSafeInteger = 9007199254640991n
4. Numeric Separators
計算過後會將區隔文字除去
const a = 1_000_000_000_000
6. Destructing Object
code:javascript
const trainer = {
name: "Satoshi",
partner: {
name: "Pikachu",
},
};
const trainerItems = {
item1: "monster ball",
item2: "bicycle",
};
const trainerStatus = { ...trainer, ...trainerItems };
console.log(trainerStatus);
/*
{
item1: "monster ball",
item2: "bicycle",
name: "Satoshi",
partner: { name: "Pikachu" }
}
*/
8. Class field
於ES2022起class宣言可以省略constructor() code:javascript
class Dog {
// before
constructor() {
this.age = 5;
}
// after
age = 5;
}
9. Class private field
class變數前方加上#可宣言為私有領域
code:javascript
class Dog {
constructor(age) {
this.#age = age;
}
showAge() {
console.log(this.#age);
}
}
const dog = new Dog(7);
dog.showAge();
// 7
// console.log(dog.#age)
// Error
// console.log(dog.age)
// undefined
10. 重新命名匯入模組後匯出
export * as name from "./foo.js";