JavaScript? ?? ?? ?? ????? ?? ? ???? ?? ??? ??? ?????. ? ????? ECMAScript 2020(ES11???? ?)? ??? ??? ??? ?????.
ECMAScript 2015(ES6??? ?)? ???? ??? JavaScript? ?? ??? ???????. ??? 2015??? ?? ??? ??? ???????. ?? ?????? ?? ??? ???? ?? ???? JavaScript ???? Babel ??? ?? ??? ??? ??? ? ????. ? ????? ECMAScript 2020(ES11)? ?? ?? ? ??? ?????.
Optional Chaining Optional Chaining
???? ???? ?? ??? ??????.
TypeError: Cannot read property 'x' of undef
TypeError: Cannot read property ‘x’ of undefined
這個(gè)錯(cuò)誤表示我們正在訪問(wèn)一個(gè)不屬于對(duì)象的屬性。
訪問(wèn)對(duì)象的屬性
const flower = { colors: { red: true } } console.log(flower.colors.red) // 正常運(yùn)行 console.log(flower.species.lily) // 拋出錯(cuò)誤:TypeError: Cannot read property 'lily' of undefined
在這種情況下,JavaScript 引擎會(huì)像這樣拋出錯(cuò)誤。但是某些情況下值是否存在并不重要,因?yàn)槲覀冎浪鼤?huì)存在。于是,可選鏈?zhǔn)秸{(diào)用就派上用場(chǎng)了!
我們可以使用由一個(gè)問(wèn)號(hào)和一個(gè)點(diǎn)組成的可選鏈?zhǔn)讲僮鞣?,去表示不?yīng)該引發(fā)錯(cuò)誤。如果沒(méi)有值,應(yīng)該返回 undefined
console.log(flower.species?.lily) // 輸出 undefined
? ?? JavaScript ????? ??? ?? ??? ?????. ??? ?? ???? ?? ??? ???? ?? ?? ??? ?? ????? ??? ???? ????. ??? ??? ?? ??? ?????!
???? ??? ??? ??? ?? ???? ???? ??? ???? ??? ?? ??? ? ????. ?? ??? undefine
? ????? ???.
let flowers = ['lily', 'daisy', 'rose'] console.log(flowers[1]) // 輸出:daisy flowers = null console.log(flowers[1]) // 拋出錯(cuò)誤:TypeError: Cannot read property '1' of null console.log(flowers?.[1]) // 輸出:undefined
??? ?????? ??? ??? ? ??? ?? ??? ??? ?? ????.
?? ???let plantFlowers = () => { return 'orchids' } console.log(plantFlowers()) // 輸出:orchids plantFlowers = null console.log(plantFlowers()) // 拋出錯(cuò)誤:TypeError: plantFlowers is not a function console.log(plantFlowers?.()) // 輸出:undefined?? ??
let number = 1 let myNumber = number || 7
Nullish Coalescing Null ? coalescing?? ??? ?? ?? ????? ?? ??? ||? ??? ?????. ?? ??? ????? ?? ??? ?????? ??? ? ????. ?? ?? ?? ?? ?? ? ?? ?????. ?? ?? ??? ??? ????? ?? ??? ?? ?? ??? ?? ?? ???? 7???.
let number = 0 let myNumber = number || 7??(??)? true??? ?? myNumber? 1? ????. ? 1. ??? ?? ??? 1? ?? 0?? ??? ????
let number = 0 let myNumber = number ?? 7
0? ?????? 0? ???????. ?? myNumber?? ???? ? 7? ?????. ??? ??? ??? ??? ?? ???. ?????? ? ?? ???? ??? ??? ?? ???? ?? ??? ???? ??? ?? ??? ???? ??? ? ????.
class Flower { #leaf_color = "green"; constructor(name) { this.name = name; } get_color() { return this.#leaf_color; } } const orchid = new Flower("orchid"); console.log(orchid.get_color()); // 輸出:green console.log(orchid.#leaf_color) // 報(bào)錯(cuò):SyntaxError: Private field '#leaf_color' must be declared in an enclosing class???? ??? ?? ?? ?? null??? ???? ?? ???? ?????. ??? ??? ?? myNumber? ?? 0? ?? ?? ????.
?? ??
???? ?? ?? ????? ????? ???? ??, ?? ?? ?? ???? ??? ? ????. ?? ??? ??? ?? ?? ??? ???? ???? ? ??, ?? ??? ?? ?????? ???? ? ???, ?? ??? ??? ???? ???? ? ????. JavaScript? ES6?? ??? ??? ????? ???? ??? ???? ???? ?????. ?? ??? ????? ?? ?? ??(#)? ?? ???.
class Flower { add_leaves() { console.log("Adding leaves"); } } const rose = new Flower(); rose.add_leaves(); Flower.add_leaves() // 拋出錯(cuò)誤:TypeError: Flower.add_leaves is not a function???? ???? ?? ??? ???? ????? ??? ?????.
?? ??
??? ???? ????? ?? ??? ?? ???? ??????? ???.
class Flower { constructor(type) { this.type = type; } static create_flower(type) { return new Flower(type); } } const rose = Flower.create_flower("rose"); // 正常運(yùn)行??????? ?? Flower ???? ???? ?????? ?? ??? ?????. ??? ?? ??? ?? ??? ???? static ???? ???? ??? ?? ???? ??? ? ????.
const func = async () => { const response = await fetch(url) }
Top Level Await Top Level Await
?? Promise ??? ??? ?? ?? Await? ???? ??, Async ???? Await? ???? ??? ???? ???.
(async () => { const response = await fetch(url) })()
???? ???? ?? ??? ???? ?? ????? ?????? ?? ?? ?? ?????. ?? ???? ?? ???(IIFE)? ???? ?? ?.
const response = await fetch(url)
??? ??? Await? ??? ??? ??? ?? ??? ??? ??? ??? ??? ????.
let Vue try { Vue = await import('url_1_to_vue') } catch { Vue = await import('url_2_to_vue) }
? ??? ?? ???? ????? ?? ??? ??? ? ?? ? ?? ?????. ?? ??? ?????. promise_1 = Promise.resolve('hello')
promise_2 = new Promise((resolve, reject) => setTimeout(reject, 200, 'problem'))
Promise.allSettled([promise_1, promise_2])
.then(([promise_1_result, promise_2_result]) => {
console.log(promise_1_result) // 輸出:{status: 'fulfilled', value: 'hello'}
console.log(promise_2_result) // 輸出:{status: 'rejected', reason: 'problem'}
})
Promise.allSettled ???
?? Promise? ??? ??? ??? ??? ? Promise.all([promise_1, promise_2])? ??? ? ????. ??? ??? ?? ? ??? ???? ??? ????? ????. ??? ??? ??? ? ?? ??? ??? ????? ????? ??? ??? ????. ??? ??? ?? ES11??? Promise.allSettled? ??????.
// Alert.js export default { show() { // 代碼 } } // 使用 Alert.js 的文件 import('/components/Alert.js') .then(Alert => { Alert.show() })??? Promise? ??? ?? ??? ??? ????, ??? Promise? ??? ??? ??? ??? ?????. ???????? ???? ?? ??????????????? ?? ????? ?? ????? ???? ?? ????. ??? ? ??? ?? ?? ??? ???????: ??
// Alert.js export default { show() { // 代碼 } } // 使用 Alert.js 的文件 import('/components/Alert.js') .then(Alert => { Alert.show() })
考慮到許多應(yīng)用程序使用諸如 webpack 之類的模塊打包器來(lái)進(jìn)行代碼的轉(zhuǎn)譯和優(yōu)化,這個(gè)特性現(xiàn)在還沒(méi)什么大作用。
MatchAll 匹配所有項(xiàng)
如果你想要查找字符串中所有正則表達(dá)式的匹配項(xiàng)和它們的位置,MatchAll 非常有用。
const regex = /\b(apple)+\b/; const fruits = "pear, apple, banana, apple, orange, apple"; for (const match of fruits.match(regex)) { console.log(match); } // 輸出 // // 'apple' // 'apple'
相比之下,matchAll 返回更多的信息,包括找到匹配項(xiàng)的索引。
for (const match of fruits.matchAll(regex)) { console.log(match); } // 輸出 // // [ // 'apple', // 'apple', // index: 6, // input: 'pear, apple, banana, apple, orange, apple', // groups: undefined // ], // [ // 'apple', // 'apple', // index: 21, // input: 'pear, apple, banana, apple, orange, apple', // groups: undefined // ], // [ // 'apple', // 'apple', // index: 36, // input: 'pear, apple, banana, apple, orange, apple', // groups: undefined // ]
globalThis 全局對(duì)象
JavaScript 可以在不同環(huán)境中運(yùn)行,比如瀏覽器或者 Node.js。瀏覽器中可用的全局對(duì)象是變量 window,但在 Node.js 中是一個(gè)叫做 global 的對(duì)象。為了在不同環(huán)境中都使用統(tǒng)一的全局對(duì)象,引入了 globalThis 。
// 瀏覽器 window == globalThis // true // node.js global == globalThis // true
BigInt
JavaScript 中能夠精確表達(dá)的最大數(shù)字是 2^53 - 1。而 BigInt 可以用來(lái)創(chuàng)建更大的數(shù)字
const theBiggerNumber = 9007199254740991n const evenBiggerNumber = BigInt(9007199254740991)
結(jié)論
我希望這篇文章對(duì)您有用,并像我一樣期待 JavaScript 即將到來(lái)的新特性。如果想了解更多,可以看看 tc39 委員會(huì)的官方Github倉(cāng)庫(kù)。
推薦教程:《JS教程》
? ??? ECMAScript 2020? ??? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)