導(dǎo)文
JavaScript ES10,也被稱為ES2019,引入了一些新的特性和語言改進(jìn)
Array.prototype.flat()和Array.prototype.flatMap()
這兩個方法可以簡化多維數(shù)組的處理。flat()方法可將多維數(shù)組展平為一維數(shù)組,而flatMap()方法在展平數(shù)組的同時還可以對每個元素執(zhí)行映射操作。
const arr = [1, 2, [3, 4, [5, 6]]];
// 使用 flat() 方法展平數(shù)組
const flattened = arr.flat();
console.log(flattened); // [1, 2, 3, 4, [5, 6]]
// 使用 flatMap() 方法展平數(shù)組并映射操作
const mappedAndFlattened = arr.flatMap(num => num * 2);
console.log(mappedAndFlattened); // [2, 4, 6, 8, 10, 12]
Object.fromEntries()
這個靜態(tài)方法允許將鍵值對列表轉(zhuǎn)換為對象。它接收一個鍵值對的可迭代對象(如數(shù)組)作為參數(shù),并返回一個新的對象。
const entries = [['name', 'John'], ['age', 30], ['city', 'New York']];
// 將鍵值對列表轉(zhuǎn)換為對象
const obj = Object.fromEntries(entries);
console.log(obj); // { name: 'John', age: 30, city: 'New York' }
String.prototype.trimStart()和String.prototype.trimEnd()
這兩個方法用于去除字符串開頭或結(jié)尾的空白字符。它們分別是trim()方法的單獨(dú)擴(kuò)展。
String.prototype.trimStart()是字符串原型對象上的一個方法,用于去除字符串開頭的空白字符(空格、制表符、換行符等)。該方法返回一個新的字符串,不改變原始字符串本身。
例如,假設(shè)有一個字符串變量:
const str = " Hello, World! ";
我們可以使用trimStart()方法去除字符串開頭的空白字符:
javascript
const trimmedStr = str.trimStart();
console.log(trimmedStr);
// 輸出: "Hello, World! "
上述代碼中,原始字符串開頭的三個空格被刪除了,得到新的字符串"Hello, World! "。
類似地,String.prototype.trimEnd()方法用于去除字符串結(jié)尾的空白字符。例如:
const str = " Hello, World! ";
const trimmedStr = str.trimEnd();
console.log(trimmedStr);
// 輸出: " Hello, World!"
在這個例子中,最后面的三個空格被移除,返回新的字符串" Hello, World!"。
需要注意的是,這兩個方法只會刪除開頭或結(jié)尾的空白字符,字符串中間的空白字符不會受到影響。如果需要同時刪除開頭和結(jié)尾的空白字符,可以直接使用原生的trim()方法:
const str = " Hello, World! ";
const trimmedStr = str.trim();
console.log(trimmedStr);
// 輸出: "Hello, World!"
String.prototype.trimStart()和String.prototype.trimEnd()是用于去除字符串開頭和結(jié)尾的空白字符的方法。它們分別返回新的字符串,并保持原始字符串不變。如果需要同時刪除開頭和結(jié)尾的空白字符,可以使用trim()方法。
格式化數(shù)字
引入了新的Number.prototype.toFixed()方法,它允許指定小數(shù)點(diǎn)后的位數(shù)并將數(shù)字四舍五入為指定精度;而Intl.NumberFormat對象提供了更靈活和本地化的數(shù)字格式化。
動態(tài)導(dǎo)入
通過import()函數(shù),可以在運(yùn)行時動態(tài)地導(dǎo)入模塊。這使得按需加載模塊變得更加容易。
// 動態(tài)導(dǎo)入模塊
import('./module.js')
.then(module => {
// 使用導(dǎo)入的模塊
module.doSomething();
})
.catch(error => {
console.error('模塊加載失敗:', error);
});
可選的catch綁定
現(xiàn)在可以在try-catch語句中省略catch塊中的綁定,只使用catch {},而不會將錯誤綁定到變量。
try {
// 執(zhí)行可能拋出異常的代碼
throw new Error('發(fā)生了錯誤');
} catch {
// 省略 catch 塊中的綁定
console.log('捕獲到錯誤');
}
BigInt
引入了一種新的基本數(shù)據(jù)類型 BigInt,它可以表示任意精度的整數(shù)。使用后綴n來聲明一個BigInt。
const bigNumber = BigInt("123456789012345678901234567890");
console.log(bigNumber); // 123456789012345678901234567890n
console.log(typeof bigNumber); // "bigint"
const added = bigNumber + 1n;
console.log(added); // 123456789012345678901234567891n
globalThis
引入了一個名為globalThis的全局屬性,它始終指向全局對象,無論在什么環(huán)境下。文章來源:http://www.zghlxwxcb.cn/news/detail-496514.html
// 在瀏覽器控制臺和 Node.js 中使用 globalThis
console.log(globalThis);
// 在瀏覽器全局作用域中聲明變量
globalThis.myVariable = "Hello World";
console.log(myVariable); // "Hello World"
這些是ES10中的一些主要特性。它們提供了更方便、更強(qiáng)大的語言功能,使JavaScript開發(fā)人員能夠更高效地編寫代碼。文章來源地址http://www.zghlxwxcb.cn/news/detail-496514.html
到了這里,關(guān)于JavaScript ES10新特性的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!