ES——2016&2017合集

一、规范

1、参数列表支持尾逗号


支持在函数声明及调用时末尾增加逗号而不报 SyntaxError 错误。

function add(a, b,) {
 return a + b;
}
add(1, 2,)

2、共享内存和原子 - Shared memory and atomics


Shared memory and atomics,“共享内存和原子” 又名 “共享数组缓冲区”,可以在主线程和多个工作线程间共享对象的字节,能更快的在多个工作线程间共享数据、除 postMessage() 多了一种数据传输方式。

多线程间的内存共享会带来一个后端常遇见的问题 “竞态条件”,提案提供了全局变量 Atomics 来解决这个问题。详情参考 ES proposal: Shared memory and atomics

二、方法

1、Array.prototype.includes


当判断一个数组中是否包含指定值时,使用 includes() 会很实用。

['a', 'b'].includes('a') // true

2、求幂运算符


  • * 是求幂运算符,左侧是基数、右侧是指数。等价于之前的 Math.pow() 函数。
2 ** 3 // 8
Math.pow(2, 3) // 8

最后关于 ES2015,也就是常说的 ES6 更新的内容是比较多的,不在这里赘述,推荐一个资料给有需要的朋友 https://es6.ruanyifeng.com/。

3、Object.values / Object.entries


Object.values() 返回一个对象的所有值,同 Object.keys() 相反。

const obj = { name: 'Tom', age: 18 }
console.log(Object.values(obj)); // [ 'Tom', 18 ]

Object.entries() 返回一个对象的键值对。

const obj = { name: 'Tom', age: 18 }
for (const [key, value] of Object.entries(obj)) {
  console.log(key, value);
}
// Output
// name Tom
// age 18

4、字符串补全


两个字符串补全方法 .padStart().padEnd() 分别在字符串的头部、尾部进行按目标长度和指定字符进行填充。

console.log('a'.padStart(5, '1')); // 1111a
console.log('a'.padEnd(5, '2')); // a2222

5、async/await

省略

6、Object.getOwnPropertyDesciptors


用来获取一个对象的所有自身属性的描述符。

const obj = {
  name: 'Tom',
  run: () => ``,
};
console.log(Object.getOwnPropertyDescriptors(obj));
// {
//   name: {
//     value: 'Tom',
//     writable: true,
//     enumerable: true,
//     configurable: true
//   },
//   run: {
//     value: [Function: run],
//     writable: true,
//     enumerable: true,
//     configurable: true
//   }
// }
posted @ 2024-08-14 07:36  巴伐利亚药水哥  阅读(12)  评论(0)    收藏  举报