1_Bnj_ZAhCL0UEmU3E_74ieA.png

原文:The Complete Guide to ES10 Features 链接:https://medium.com/@js_tut/the-complete-guide-to-es10-features-f09a8c7be1bd 翻译:印象卡卡

ES10仍然只是一个草案, 但是除了Object.fromEntries大多数功能已经在Chrome中实现,为什么不尽早开始探索一番呢? 当浏览器支持它的时候,你将走在技术前沿。 等待浏览器支持只是时间问题。 对于有兴趣探索ES10的人来说,这是一份简要的指南。
ES10没有ES6那样巨大的特性变化但是也添加了很多有趣的特性:
**
⬬ String .matchAll()
⬬ Dynamic import()
⬬ Array .flat() .flatMap()
⬬ Object .fromEntries()
⬬ String .trimStart() .trimEnd()
⬬ well-formed JSON.stringify()
⬬ stable Array .sort()
⬬ new Function .toString()
⬬ Standardized globalThis object

BigInt — 任意精度的整数

BigInt是第7种原始数据类型

BigInt是一个任意精度的整数,意味着变量可以定义2⁵³的数字了,不再有9007199254740992的最大限制。

  1. const b = 1n; // 定义一个bigint


之前大于9007199254740992的整数是不支持的,超过限制之后值会锁定为
MAX_SAFE_INTEGER + 1

  1. const limit = Number.MAX_SAFE_INTEGER;
  2. 9007199254740991
  3. limit + 1;
  4. 9007199254740992
  5. limit + 2;
  6. 9007199254740992 <--- MAX_SAFE_INTEGER + 1 exceeded
  7. const larger = 9007199254740991n;
  8. 9007199254740991n
  9. const integer = BigInt(9007199254740991); // 使用数字初定义
  10. 9007199254740991n
  11. const same = BigInt("9007199254740991"); // 使用字符串定义
  12. 9007199254740991n

typeof

  1. typeof 10;
  2. 'number'
  3. typeof 10n;
  4. 'bigint'

两种类型可以使用等于号比较

  1. 10n === BigInt(10);
  2. true
  3. 10n == 10;
  4. true

两种类型不可以使用数学运算符

  1. 200n / 10n
  2. 20n
  3. 200n / 20
  4. Uncaught TypeError:
  5. Cannot mix BigInt and other types, use explicit conversions <

前边可以加“-”号,不可以加“+”号

  1. -100n
  2. -100n
  3. +100n
  4. Uncaught TypeError:
  5. Cannot convert a BigInt value to a number

String.prototype.matchAll()

String.match只返回第一个匹配的元素

  1. let string = 'Helle';
  2. let matches = string.match('l');
  3. console.log(matches[0]); // "l"

使用.matchAll()

  1. // Match all occurrences of the letters: "e" or "l"
  2. let iterator = "hello".matchAll(/[el]/);
  3. for (const match of iterator)
  4. console.log(match);
  5. // result
  6. [ 'e', index: 1, input: 'hello' ] // Iteration 1
  7. [ 'l', index: 2, input: 'hello' ] // Iteration 2
  8. [ 'l', index: 3, input: 'hello' ] // Iteration 3

Dynamic import

现在经常用的动态引入js

  1. element.addEventListener('click', async () => {
  2. const module = await import(`./api-scripts/button-click.js`);
  3. module.clickEvent();
  4. });

Array.prototype.flat()

扁平化数组

  1. let multi = [1,2,3,[4,5,6,[7,8,9,[10,11,12]]]];
  2. multi.flat(); // [1,2,3,4,5,6,Array(4)]
  3. multi.flat().flat(); // [1,2,3,4,5,6,7,8,9,Array(3)]
  4. multi.flat().flat().flat(); // [1,2,3,4,5,6,7,8,9,10,11,12]
  5. multi.flat(Infinity); // [1,2,3,4,5,6,7,8,9,10,11,12]

Array.prototype.flatMap()

相当于array.map(x => [x, x 2, x x]).flat()

  1. let array = [1, 2, 3, 4, 5];
  2. array.map(x => [x, x * 2]);
  3. // result
  4. [Array(2), Array(2), Array(2)]
  5. 0: (2)[1, 2]
  6. 1: (2)[2, 4]
  7. 2: (2)[3, 6]
  8. 3: (2)[4, 8]
  9. 4: (2)[5, 10]
  10. array.flatMap(v => [v, v * 2]);
  11. // result
  12. [1, 2, 2, 4, 3, 6, 4, 8, 5, 10]

Object.fromEntries()

相当于Object.entries()的逆运算

  1. let obj = { apple : 10, orange : 20, banana : 30 };
  2. let entries = Object.entries(obj);
  3. entries;
  4. (3) [Array(2), Array(2), Array(2)]
  5. 0: (2) ["apple", 10]
  6. 1: (2) ["orange", 20]
  7. 2: (2) ["banana", 30]
  8. let fromEntries = Object.fromEntries(entries);
  9. { apple: 10, orange: 20, banana: 30 }

String.prototype.trimStart() & String.prototype.trimEnd()

trim可以指定开头还是结尾了。

  1. let greeting = " Space around ";
  2. greeting.trimEnd(); // " Space around";
  3. greeting.trimStart(); // "Space around ";

稳定的Array.prototype.sort()

之前的V8使用的不稳定的快排算法对于超过10个元素的数组。

新的Function.toString()

方法也是对象,对象都有toString()方法因为Object.prototype.toString(),所有对象都继承这个方法。
ES10标准化了所有对象和内置方法的字符串表示。

可选的catch参数

之前的catch()需要error参数,ES10里是可选的。

  1. try {
  2. JSON.parse(text);
  3. return true;
  4. }
  5. catch
  6. {
  7. return false;
  8. }

标准化的globalThis对象

之前的全局对象不是标准化的,为了兼容需要写这样的代码:

  1. if (typeof self !== 'undefined') { return self; }
  2. if (typeof window !== 'undefined') { return window; }
  3. if (typeof global !== 'undefined') { return global; }
  4. throw new Error('unable to locate global object');
  5. };

ES10添加了 globalThis 对象来访问全局在所有平台上

  1. // Access global array constructor
  2. globalThis.Array(0, 1, 2);
  3. [0, 1, 2]
  4. // Similar to window.v = { flag: true } in <= ES5
  5. globalThis.v = { flag: true };
  6. console.log(globalThis.v);
  7. { flag: true }

Symbol.description

description 是一个只读的属性返回Symbol对象的描述信息

  1. let mySymbol = 'My Symbol';
  2. let symObj = Symbol(mySymbol);
  3. symObj; // Symbol(My Symbol)
  4. String(symObj) === `Symbol(${mySymbol})`); // true
  5. symObj.description; // "My Symbol"

ES10 Classes: private, static & public members

新的语法字符# octothorpe(hash tag)现在用于直接在类里定义变量,函数,getter和setter ……以及构造函数和类方法。

  1. class Raven extends Bird {
  2. #state = { eggs: 10};
  3. // getter
  4. get #eggs() {
  5. return state.eggs;
  6. }
  7. // setter
  8. set #eggs(value) {
  9. this.#state.eggs = value;
  10. }
  11. #lay() {
  12. this.#eggs++;
  13. }
  14. constructor() {
  15. super();
  16. this.#lay.bind(this);
  17. }
  18. #render() {
  19. /* paint UI */
  20. }
  21. }