ECMAScript常用整理

ECMAScript 2019 (ES10)

Array.prototype.flat()
用于数组扁平化,递归地将数组展平到我们指定的深度。如果未指定depth参数,则默认值为1。

var newArray = arr.flat([depth]);
depth 选择性,指定巢状阵列展开的深度。预设为1
var arr1 = [1, 2, [3, 4]];
arr1.flat(); 
// [1, 2, 3, 4]

var arr2 = [1, 2, [3, 4, [5, 6]]];
arr2.flat();
// [1, 2, 3, 4, [5, 6]]

var arr3 = [1, 2, [3, 4, [5, 6]]];
arr3.flat(2);
// [1, 2, 3, 4, 5, 6]

Array.prototype.flatMap()
相当于把一个数组先调用完map函数再调用flat函数将其扁平化

重写toString()方法
允许输出其中的注释内容

可选的捕获

// Before                     // ES2019
try {                         try {
   ...                           ...
} catch(error) {              } catch {
   ...                           ...
}                             }

https://zhuanlan.zhihu.com/p/95817152

ECMAScript 2018 (ES9)

异步迭代
使用一个for-await-of循环,该循环将可迭代对象转换为Promise,

ECMAScript 2017 (ES8)

Async Functions (异步函数)
Async Await

Object.getOwnPropertyDescriptors()
getOwnPropertyDescriptors 方法返回一指定对象自己所有的属性内容,并且属性内容只是自身直接定义的,而不是从object的原型继承而来的。

padStart 和 padEnd
对字符串的开发或结尾进行填充,从而使字符串获得给定的长度。你可以用特定的字符、字符串或者是空格(默认)来填充

Object.entries() 和 Object.values()
Object.values方法返回一个指定对象可枚举属性值的数组
Object.entries 方法返回一个给定对象可枚举属性值的数组[key, value]

const family = {
  father: "Jonathan Kent",
  mother: "Martha Kent",
  son: "Clark Kent",
}
Object.keys(family); // ["father", "mother", "son"]
Object.values(family); // ["Jonathan Kent", "Martha Kent", "Clark Kent"]
Object.entries(family); 
// (3) [Array(2), Array(2), Array(2)]
// 0: (2) ["father", "Jonathan Kent"]
// 1: (2) ["mother", "Martha Kent"]
// 2: (2) ["son", "Clark Kent"]

函数参数列表和调用后面的逗号
在函数参数尾部使用逗号时不会再触发错误警告(SyntaxError)

ECMAScript 2016 (ES7)

只增加了两个新功能,很小的一次改动

Array.prototype.includes()
检查数组是否包含某个元素项,存在返回true,否则将返回false。

指数运算符
求幂运算符 ** 等价于 Math.pow()。

Math.pow(4,2== 4 ** 2

参考
https://blog.csdn.net/i15730384741/article/details/107513323
https://inspiredwebdev.com/everything-from-es-2016-to-es-2019

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值