ecmascript
ECMAScript (ES for short) is a scripting language specification standardized by ECMA International in ECMA-262 and ISO/IEC 16262. It was created to standardize the JavaScript language, so as to foster multiple independent standard implementations from browser vendors. It evolves every year with new features.
ECMAScript (简称ES)是ECMA International在ECMA-262和ISO / IEC 16262中标准化的脚本语言规范。创建该规范是为了对JavaScript语言进行标准化,从而促进浏览器供应商的多种独立标准实现。 它每年都有新功能发展。
The 2019 edition of the ECMAScript specification saw the addition of many new features, and here I’ll cover some of these new features. I personally love how javaScript keeps evolving and improving on a regular basis.
ECMAScript规范的2019年版增加了许多新功能,在这里我将介绍其中一些新功能。 我个人喜欢javaScript如何保持不断发展和改进。
Array.flat()
returns a new array with any sub-array(s) flattened. A call to Array.flat()
without any arguments will only flatten one-level deep. An optional depth argument can be provided or it can just be called consecutively.
Array.flat()
返回一个新数组,该数组的所有子数组都被展平。 不带任何参数的Array.flat()
调用Array.flat()
将其Array.flat()
。 可以提供可选的depth参数,也可以仅连续调用它。
let arr = [1, 2, 3, [4, 5, 6, [7, 8, 9, [10, 11, 12]]]];
arr.flat(); // [1, 2, 3, 4, 5, 6, Array(4)];
arr.flat().flat(); // [1, 2, 3, 4, 5, 6, 7, 8, 9, Array(3)];
arr.flat(3); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
// Or, if you're not sure about the depth of the array:
arr.flat(Infinity); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
The flatMap()
method is identical to the ES6 map method, but also flattens at the same time. The flatMap()
method first maps each element using a mapping function, then flattens the result into a new array. flatMap()
is often quite useful, as merging both into one method is slightly more efficient.
flatMap()
方法与ES6地图方法相同,但同时也可以展平。 flatMap()
方法首先使用映射函数映射每个元素,然后将结果展平为新数组。 flatMap()
通常非常有用,因为将两者合并为一种方法效率更高。
let arr = [1, 2, 3, 4, 5];
arr.map(x => [x, x * 2]);
// [Array(2), Array(2), Array(2)]
// 0: (2)[1, 2]
// 1: (2)[2, 4]
// 2: (2)[3, 6]
arr.flatMap(v => [v, v * 2]);
// [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]
String.trimStart()
can be used to trim white space from the start of a string.
String.trimStart()
可用于从字符串开头修剪空白。
let greeting = " Hello everyone";
console.log(greeting.trimStart());
// "Hello everyone"
let greeting = "Hello world ";
console.log(greeting.trimEnd());
// "Hello world"
Optional catch binding allows developers to use try/catch without the error parameter inside the catch block.
可选的catch绑定使开发人员可以在try代码块中使用try / catch而不使用error参数。
Before ES2019 we use:
在ES2019之前,我们使用:
try {
// some code
}
catch (err) {
// error handling code
}
Now we can use try/catch like this with ES2019:
现在我们可以在ES2019中使用try / catch这样的方法:
try {
// some code
}
catch {
// error handling code
}
It creates an object or transforms key-value pairs into an object. It only accepts iterables e.g: Object.fromEntries(someIterable)
.
它创建一个对象或将键值对转换为一个对象。 它仅接受可迭代对象,例如: Object.fromEntries(someIterable)
。
let entries = new Map([["name", "john"], ["age", 22]]);
console.log(Object.fromEntries(entries));
// { name: 'john', age: 22 }
The read-only description property is a string returning the optional description of Symbol objects.
只读description属性是一个字符串,返回Symbol对象的可选描述。
let mySymbol = `My Symbol`;
let symObj = Symbol(mySymbol);
console.log(symObj) // Symbol(mySymbol);
console.log(String(symObj) === `Symbol(${mySymbol})`); // true
console.log(symObj.description); // "My Symbol"
翻译自: https://www.digitalocean.com/community/tutorials/js-es2019
ecmascript