您是否听说过ES – ES 10 / ES 2019 的JavaScript 中的新功能。 镀铬72 提出了很多全新的功能,其中之一是ES10的新功能,也是我在上一篇文章中提到的功能之一,即 原生延迟加载Chrome新功能.
array.flat()
let’s first start with the array.flat()
method what is exactly does. array.flat()
will flattens the array by given depth. the flat(2)
method accepts the parameter as depth to flatten the array.array.flat(Infinity)
: the 在 finity
flattens array to the last depth of array.
如果您曾经使用过 Lodash then you must be familiar with the features _.flatten
or _.flattenDepth
.
const someNumber = [12,3,3,[33,3,4,55,[34,6,7,9,0]]];
console.log(someNumber.flat()); // Default flat level is 1
// output : [12, 3, 3, 33, 3, 4, 55, Array(5)]
console.log(someNumber.flat(Infinity));
// output : [12, 3, 3, 33, 3, 4, 55, 34, 6, 7, 9, 0]
array.flatMap()
The another thing you can do now to play with array is you can call .flatMap()
on them.
As the name only says that it flat
and then map
method allows us to map through the items in an array and then flatten the map array in just single method.
Example using .map()
method
const numbers = [1,2,3,4,5,6];
const alphaNumber = ['one', 'two', 'three', 'four', 'five', 'six'];
const mappedExample = numbers.map((number, index) => [number, alphaNumber[index]]);
console.log(mappedExample);
// output : [[1,'one'], [2,'two'], [3, 'three'], [4,'four'], [5, 'five'], [6, 'six']]
Example using .flatMap()
method
const numbers = [1,2,3,4,5,6];
const alphaNumber = ['one', 'two', 'three', 'four', 'five', 'six'];
const mappedAndFlattenedExample = numbers.flatMap((number, index) => [
number,
alphaNumber[index]
]);
console.log(mappedAndFlattenedExample);
// output : [1, 'one', 2, 'two', 3, 'three', 4, 'four', 5, 'five', 6, 'six']
因此,我们从上面的示例中得到了一个数字数组,还提供了第二组具有字母数字的数组。
保持不变,我们要合并两个数组,因此在每个数字之后将有一个字母数字。
为此,我们必须在带索引的numbers数组中进行映射,并且对于每个数字,我们将在同一索引中返回合并数字和字母数字的数组。
然后,如果我们将其记录下来,则可以在控制台中看到,我们将获得一个二维数字数组和字母数字。
in other example we have change map()
to flatMap()
, then we have get one dimensional array of numbers and the alphabetical number.
string.trimStart()
/ string.trimEnd()
String prototype methods have also got some new updates, now we can trim whitespaces from starting or the ending of the string using string.trimStart()
and string.trimEnd()
methods.
const description = " I got some whitespaces :) ";
console.log(description.trimLeft());
// output : "I got some whitespaces :) ";
console.log(description.trimRight());
// output : " I got some whitespaces :)";
Object.fromEntries
Java脚本 对象现在具有一个新的湖北福彩名称,如 fromEntries()
const numberNames = [[1,'one'], [2,'two'], [3, 'three'], [4,'four'], [5, 'five'], [6, 'six']];
const numberNamesObject = Object.fromEntries (numberNames);
console.log(numberNamesObject);
// output : {1: "one", 2: "two", 3: "three", 4: "four", 5: "five", 6: "six"}
Now you can take arrays of entries and form an object from it. Previously we had only the Object.entries
which was taking an object and was returning an array of [key, value
] pairs.
可选的捕捉绑定
它提供的另一个功能是不将错误参数绑定到catch子句的能力。
try {
throw "Error not bothered about";
} catch (error) {
// your Tricky logic..
}
So incase if you dont need the error
parameter you can just avoid it.
try {
throw "Error not bothered about";
} catch {
// your Tricky logic..
}
这很有争议,您可能想阅读作者的广泛文章 阿克塞尔(Axel Rauschmayer) 他在其中提到了此湖北福彩的含义以及一些替代湖北福彩。
函数toString修订
在 ES 6 when toString
was invoked on a function it was returning string representation of that perticular function depending on ECMAScript engine.
当可以编译时,它将返回源代码。否则–一个标准化的占位符。
so if toString
couldn’t create syntactically valid Java脚本 code, it would return a string for which eval throws a SyntaxError. It was a requirement so that eval couldn’t parse this string.
问题在于您永远无法百分百确定ECMAScript的未来版本不会使其在语法上有效。
New proposal standardizes the placeholder: a function with body { [native code] }
.
For functions defined with Java脚本 code toString
will return their original source code:
function geekstrick() {
// Hello, I'm an ordinary function
}
console.log(geekstrick.toString());
// Output :
// function geekstrick() {
// // Hello, I'm an ordinary function
// }
Built-in function objects, bound function exotic objects and callable objects not defined in Java脚本 code will return a NativeFunction
string.
console.log(isNaN.toString());
// function isNaN() { [native code] }
console.log(Math.pow.toString());
// function pow() { [native code] }
console.log(function foo() {}.bind(null).toString());
// function () { [native code] }