ArrayBuffer 是一個低階資料結構,代表一塊記憶體。它用於需要直接處理二進位資料的上下文,例如影像處理、檔案處理或 WebAssembly。
常見用例:
ArrayBuffer 建立時大小固定,建立後無法修改。這意味著調整大小需要建立新的緩衝區並手動複製資料。
let buffer = new ArrayBuffer(10); // Fixed size of 10 bytes // To "resize", you had to create a new ArrayBuffer: let newBuffer = new ArrayBuffer(15); new Uint8Array(newBuffer).set(new Uint8Array(buffer));
您可以建立可調整大小的 ArrayBuffer 對象,並可以選擇調整大小並傳輸其內容,使它們能夠更靈活地處理可變長度資料。
擴充範例:
let buffer = new ArrayBuffer(10, { maxByteLength: 20 }); console.log(buffer.byteLength); // Initial length: 10 bytes // Resize the buffer buffer.resize(15); console.log(buffer.byteLength); // Resized length: 15 bytes // Transfer the buffer's contents to a new buffer with a different length let newBuffer = buffer.transfer(5); console.log(newBuffer.byteLength); // Transferred length: 5 bytes
優點:
缺點:
正規表示式 (regex) 是形成搜尋模式的字元序列。它們通常用於搜尋、替換和驗證字串等任務。
常見用例:
正規表示式集在表示複雜字元範圍或組合方面受到限制。建立特定的集合需要詳細的模式。
// Matching "a", "c", "d", or "e" required explicit listing: let regex = /[acd]|e/; console.log(regex.test("d")); // true
/v 標誌引入了高級集合操作,允許更具表現力的模式。
擴充範例:
// Using the /v flag for advanced sets let regex = /[a[c-e]]/v; // Matches "a", "c", "d", or "e" console.log(regex.test("d")); // true console.log(regex.test("b")); // false
優點:
缺點:
Promise 是一個表示非同步操作最終完成或失敗的物件。與傳統的基於回調的程式碼相比,它允許開發人員編寫非同步程式碼,以更具可讀性的方式處理最終結果。
常見用例:
在Promise建構函式之外手動處理resolve和reject函式很麻煩。
let buffer = new ArrayBuffer(10); // Fixed size of 10 bytes // To "resize", you had to create a new ArrayBuffer: let newBuffer = new ArrayBuffer(15); new Uint8Array(newBuffer).set(new Uint8Array(buffer));
Promise.withResolvers 透過直接傳回包含promise、resolve 和reject 函數的物件來簡化此模式。
擴充範例:
let buffer = new ArrayBuffer(10, { maxByteLength: 20 }); console.log(buffer.byteLength); // Initial length: 10 bytes // Resize the buffer buffer.resize(15); console.log(buffer.byteLength); // Resized length: 15 bytes // Transfer the buffer's contents to a new buffer with a different length let newBuffer = buffer.transfer(5); console.log(newBuffer.byteLength); // Transferred length: 5 bytes
優點:
缺點:
分組是指根據共享屬性或標準將資料分類為集合的過程。它簡化了數據分析和呈現。
常見用例:
將資料分組通常涉及使用reduce()方法或自訂邏輯,這可能很冗長且重複。
// Matching "a", "c", "d", or "e" required explicit listing: let regex = /[acd]|e/; console.log(regex.test("d")); // true
Object.groupBy 和 Map.groupBy 簡化資料分組。
擴充範例:
// Using the /v flag for advanced sets let regex = /[a[c-e]]/v; // Matches "a", "c", "d", or "e" console.log(regex.test("d")); // true console.log(regex.test("b")); // false
優點:
缺點:
原子允許對多個工作人員可以存取的共享記憶體進行操作。這對於並行計算至關重要,因為任務需要同步且不受競爭條件的影響。
常見用例:
等待共享記憶體的變更必須同步完成,或需要複雜的解決方法程式碼。
let buffer = new ArrayBuffer(10); // Fixed size of 10 bytes // To "resize", you had to create a new ArrayBuffer: let newBuffer = new ArrayBuffer(15); new Uint8Array(newBuffer).set(new Uint8Array(buffer));
Atomics.waitAsync 允許開發人員非同步等待共享記憶體的更改,從而簡化並發性。
擴充範例:
let buffer = new ArrayBuffer(10, { maxByteLength: 20 }); console.log(buffer.byteLength); // Initial length: 10 bytes // Resize the buffer buffer.resize(15); console.log(buffer.byteLength); // Resized length: 15 bytes // Transfer the buffer's contents to a new buffer with a different length let newBuffer = buffer.transfer(5); console.log(newBuffer.byteLength); // Transferred length: 5 bytes
優點:
缺點:
Unicode 是一種文字表示標準,可確保不同語言和腳本的字元可以跨平台一致地表示。有時,Unicode 字串可能存在諸如不成對的代理字元之類的問題,這可能會導致錯誤或意外行為。
常見問題:
確保格式良好的 Unicode 字串需要自訂檢查和轉換。
// Matching "a", "c", "d", or "e" required explicit listing: let regex = /[acd]|e/; console.log(regex.test("d")); // true
新的 isWellFormed 和 toWellFormed 方法可以輕鬆檢查和修復字串。
擴充範例:
// Using the /v flag for advanced sets let regex = /[a[c-e]]/v; // Matches "a", "c", "d", or "e" console.log(regex.test("d")); // true console.log(regex.test("b")); // false
優點:
缺點:
Feature | Description | Example |
---|---|---|
Resizable ArrayBuffer | Allows resizing of ArrayBuffer objects. | buffer.resize(15); |
Transferable ArrayBuffer | Enables transferring ArrayBuffer without copying data. | let newBuffer = buffer.transfer(5); |
/v Flag for RegExp | Supports advanced set operations in regex. | /[a[c-e]]/v |
Promise.withResolvers | Simplifies custom Promise construction. | let { promise, resolve } = Promise.withResolvers(); |
Object.groupBy and Map.groupBy | Groups data by a callback result. | Object.groupBy(array, item => item[0]); |
Atomics.waitAsync | Asynchronously waits on shared memory changes. | Atomics.waitAsync(int32, 0, 0).value.then(...); |
String.isWellFormed and toWellFormed | Checks and fixes Unicode strings for well-formedness. | str.isWellFormed(); str.toWellFormed(); |
以上是探索 ECMAScript 更新:初學者指南的詳細內容。更多資訊請關注PHP中文網其他相關文章!