Array.prototype.flat() / flatMap()

  1. # Array.prototype.flat() / flatMap()
  2. flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。
  3. flatMap()与 map() 方法和深度为1 flat() 几乎相同.,不过它会首先使用映射函数映射每个元素,然后将结果压缩成一个新数组,这样效率会更高。
  4. 例子如下:
  5. var arr1 = [1, 2, 3, 4]
  6. arr1.map(x => [x * 2]) // [[2], [4], [6], [8]]
  7. arr1.flatMap(x => [x * 2]) // [2, 4, 6, 8]
  8. // 深度为1
  9. arr1.flatMap(x => [[x * 2]]) // [[2], [4], [6], [8]]
  10. flatMap()可以代替reduce() concat(),例子如下:
  11. var arr = [1, 2, 3, 4]
  12. arr.flatMap(x => [x, x * 2]) // [1, 2, 2, 4, 3, 6, 4, 8]
  13. // 等价于
  14. arr.reduce((acc, x) => acc.concat([x, x * 2]), []) // [1, 2, 2, 4, 3, 6, 4, 8]
  15. 但这是非常低效的,在每次迭代中,它创建一个必须被垃圾收集的新临时数组,并且它将元素从当前的累加器数组复制到一个新的数组中,而不是将新的元素添加到现有的数组中。

String.prototype.trimStart() / trimLeft() / trimEnd() / trimRight()

  1. # String.prototype.trimStart() / trimLeft() / trimEnd() / trimRight()
  2. ES5中,我们可以通过trim()来去掉字符首尾的空格,但是却无法只去掉单边的,但是在ES10之后,我们可以实现这个功能。
  3. 如果我们要去掉开头的空格,可以使用trimStart()或者它的别名trimLeft(),
  4. 同样的,如果我们要去掉结尾的空格,我们可以使用trimEnd()或者它的别名trimRight()。
  5. 例子如下:
  6. const Str = ' Hello world! '
  7. console.log(Str) // ' Hello world! '
  8. console.log(Str.trimStart()) // 'Hello world! '
  9. console.log(Str.trimLeft()) // 'Hello world! '
  10. console.log(Str.trimEnd()) // ' Hello world!'
  11. console.log(Str.trimRight()) // ' Hello world!'
  12. 不过这里有一点要注意的是,trimStart()跟trimEnd()才是标准方法,trimLeft()跟trimRight()只是别名。
  13. 在某些引擎里(例如Chrome),有以下的等式:
  14. String.prototype.trimLeft.name === "trimStart"
  15. String.prototype.trimRight.name === "trimEnd"

Object.fromEntries()

  1. # Object.fromEntries()
  2. Object.fromEntries() 方法把键值对列表转换为一个对象,它是Object.entries()的反函数。
  3. 例子如下:
  4. const entries = new Map([
  5. ['foo', 'bar'],
  6. ['baz', 42]
  7. ])
  8. const obj = Object.fromEntries(entries)
  9. console.log(obj) // Object { foo: "bar", baz: 42 }

Symbol.prototype.description

  1. # Symbol.prototype.description
  2. description 是一个只读属性,它会返回Symbol对象的可选描述的字符串。与 Symbol.prototype.toString() 不同的是它不会包含Symbol()的字符串。例子如下:
  3. Symbol('desc').toString(); // "Symbol(desc)"
  4. Symbol('desc').description; // "desc"
  5. Symbol('').description; // ""
  6. Symbol().description; // undefined
  7. // 具名 symbols
  8. Symbol.iterator.toString(); // "Symbol(Symbol.iterator)"
  9. Symbol.iterator.description; // "Symbol.iterator"
  10. //全局 symbols
  11. Symbol.for('foo').toString(); // "Symbol(foo)"
  12. Symbol.for('foo').description; // "foo"

String.prototype.matchAll

  1. # String.prototype.matchAll
  2. matchAll() 方法返回一个包含所有匹配正则表达式的结果及分组捕获组的迭代器。并且返回一个不可重启的迭代器。例子如下:
  3. var regexp = /t(e)(st(\d?))/g
  4. var str = 'test1test2'
  5. str.match(regexp) // ['test1', 'test2']
  6. str.matchAll(regexp) // RegExpStringIterator {}
  7. [...str.matchAll(regexp)] // [['test1', 'e', 'st1', '1', index: 0, input: 'test1test2', l

Function.prototype.toString() 返回注释与空格

  1. # Function.prototype.toString() 返回注释与空格
  2. 在以往的版本中,Function.prototype.toString()得到的字符串是去掉空白符号的,但是从ES10开始会保留这些空格,如果是原生函数则返回你控制台看到的效果,例子如下:
  3. function sum(a, b) {
  4. return a + b;
  5. }
  6. console.log(sum.toString())
  7. // "function sum(a, b) {
  8. // return a + b;
  9. // }"
  10. console.log(Math.abs.toString()) // "function abs() { [native code] }"

try-catch

  1. # 在以往的版本中,try-catchcatch后面必须带异常参数,例如:
  2. // ES10之前
  3. try {
  4. // tryCode
  5. } catch (err) {
  6. // catchCode
  7. }
  8. 但是在ES10之后,这个参数却不是必须的,如果用不到,我们可以不用传,例如:
  9. try {
  10. console.log('Foobar')
  11. } catch {
  12. console.error('Bar')
  13. }

BigInt

  1. # BigInt
  2. BigInt 是一种内置对象,它提供了一种方法来表示大于 253 - 1 的整数。这原本是 Javascript中可以用 Number 表示的最大数字。BigInt 可以表示任意大的整数。
  3. 可以用在一个整数字面量后面加 n 的方式定义一个 BigInt ,如:10n,或者调用函数BigInt()。
  4. 在以往的版本中,我们有以下的弊端:
  5. // 大于2的53次方的整数,无法保持精度
  6. 2 ** 53 === (2 ** 53 + 1)
  7. // 超过2的1024次方的数值,无法表示
  8. 2 ** 1024 // Infinity
  9. 但是在ES10引入BigInt之后,这个问题便得到了解决。
  10. 以下操作符可以和 BigInt 一起使用: +、*、-、**、% 。除 >>> (无符号右移)之外的位操作也可以支持。因为 BigInt 都是有符号的, >>> (无符号右移)不能用于 BigIntBigInt 不支持单目 (+) 运算符。
  11. / 操作符对于整数的运算也没问题。可是因为这些变量是 BigInt 而不是 BigDecimal ,该操作符结果会向零取整,也就是说不会返回小数部分。
  12. BigInt Number不是严格相等的,但是宽松相等的。
  13. 所以在BigInt出来以后,JS的原始类型便增加到了7个,如下:
  14. BooleanNullUndefinedNumberStringSymbol (ES6)•BigInt (ES10)

globalThis

  1. # globalThis
  2. globalThis属性包含类似于全局对象 this值。所以在全局环境下,我们有:
  3. globalThis === this // true

import()

  1. # import()
  2. 静态的import 语句用于导入由另一个模块导出的绑定。无论是否声明了 严格模式,导入的模块都运行在严格模式下。在浏览器中,import 语句只能在声明了 type="module" script 的标签中使用。
  3. 但是在ES10之后,我们有动态 import(),它不需要依赖 type="module" script标签。
  4. 所以我们有以下例子:
  5. const main = document.querySelector("main")
  6. for (const link of document.querySelectorAll("nav > a")) {
  7. link.addEventListener("click", e => {
  8. e.preventDefault()
  9. import('/modules/my-module.js')
  10. .then(module => {
  11. module.loadPageInto(main);
  12. })
  13. .catch(err => {
  14. main.textContent = err.message;
  15. })
  16. })
  17. }

私有元素与方法

  1. # ES10之前,如果我们要实现一个简单的计数器组件,我们可能会这么写:
  2. // web component 写法
  3. class Counter extends HTMLElement {
  4. get x() {
  5. return this.xValue
  6. }
  7. set x(value) {
  8. this.xValue = value
  9. window.requestAnimationFrame(this.render.bind(this))
  10. }
  11. clicked() {
  12. this.x++
  13. }
  14. constructor() {
  15. super()
  16. this.onclick = this.clicked.bind(this)
  17. this.xValue = 0
  18. }
  19. connectedCallback() {
  20. this.render()
  21. }
  22. render() {
  23. this.textContent = this.x.toString()
  24. }
  25. }
  26. window.customElements.define('num-counter', Counter)
  27. 但是在ES10之后我们可以使用私有变量进行组件封装,如下:
  28. class Counter extends HTMLElement {
  29. #xValue = 0
  30. get #x() {
  31. return #xValue
  32. }
  33. set #x(value) {
  34. this.#xValue = value
  35. window.requestAnimationFrame(this.#render.bind(this))
  36. }
  37. #clicked() {
  38. this.#x++
  39. }
  40. constructor() {
  41. super();
  42. this.onclick = this.#clicked.bind(this)
  43. }
  44. connectedCallback() {
  45. this.#render()
  46. }
  47. #render() {
  48. this.textContent = this.#x.toString()
  49. }
  50. }
  51. window.customElements.define('num-counter', Counter)