Skip to content

ES8

版本记录

版本介绍

来源:在线规范 Introduction

This specification introduces Async Functions, Shared Memory, and Atomics along with smaller language and library enhancements, bug fixes, and editorial updates. Async functions improve the asynchronous programming experience by providing syntax for promise-returning functions. Shared Memory and Atomics introduce a new memory model that allows multi-agent programs to communicate using atomic operations that ensure a well-defined execution order even on parallel CPUs. This specification also includes new static methods on Object: Object.values, Object.entries, and Object.getOwnPropertyDescriptors.

版本概要

版本特性

Object.values()/Object.entries()

Object.values()

Object.values() 静态方法返回一个给定对象的自有可枚举字符串键属性值组成的数组。

js
const object1 = {
  a: 'somestring',
  b: 42,
  c: false,
}

console.log(Object.values(object1))
// Expected output: Array ["somestring", 42, false]

Object.entries()

Object.entries() 静态方法返回一个数组,包含给定对象自有的可枚举字符串键属性的键值对。

js
const object1 = {
  a: 'somestring',
  b: 42,
}

for (const [key, value] of Object.entries(object1)) {
  console.log(`${key}: ${value}`)
}

// Expected output:
// "a: somestring"
// "b: 42"

Object.getOwnPropertyDescriptors()

Object.getOwnPropertyDescriptor() 静态方法返回一个对象,该对象描述给定对象上特定属性(即直接存在于对象上而不在对象的原型链中的属性)的配置。返回的对象是可变的,但对其进行更改不会影响原始属性的配置。

js
const object1 = {
  property1: 42,
}

const descriptor1 = Object.getOwnPropertyDescriptor(object1, 'property1')

console.log(descriptor1.configurable)
// Expected output: true

console.log(descriptor1.value)
// Expected output: 42

String.padding

String.prototype.padStart()

padStart() 方法用另一个字符串填充当前字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的开头开始的。

js
const str1 = '5'

console.log(str1.padStart(2, '0'))
// Expected output: "05"

const fullNumber = '2034399002125581'
const last4Digits = fullNumber.slice(-4)
const maskedNumber = last4Digits.padStart(fullNumber.length, '*')

console.log(maskedNumber)
// Expected output: "************5581"

String.prototype.padEnd()

padEnd() 方法会将当前字符串从末尾开始填充给定的字符串(如果需要会重复填充),直到达到给定的长度。填充是从当前字符串的末尾开始的。

js
const str1 = 'Breaded Mushrooms'

console.log(str1.padEnd(25, '.'))
// Expected output: "Breaded Mushrooms........"

const str2 = '200'

console.log(str2.padEnd(5))
// Expected output: "200  "

函数参数尾逗号

尾后逗号 (有时叫做“终止逗号”)在向 JavaScript 代码添加元素、参数、属性时十分有用。如果你想要添加新的属性,并且上一行已经使用了尾后逗号,你可以仅仅添加新的一行,而不需要修改上一行。这使得版本控制的代码比较(diff)更加清晰,代码编辑过程中遇到的麻烦更少。

JavaScript 一开始就支持数组字面量中的尾后逗号,随后向对象字面量(ECMAScript 5)中添加了尾后逗号。ECMAScript 2017,又将其添加到函数参数中。

但是,JSON 不允许尾后逗号。

js
function f(p) {}
function f(p) {}

;(p) => {}
;(p) => {}

// 尾后逗号也可用于类或对象的方法定义。
class C {
  one(a) {}
  two(a, b) {}
}

const obj = {
  one(a) {},
  two(a, b) {},
}

// 函数调用
// 下面的两个函数调用都是合法的,并且互相等价。
f(p)
f(p)

Math.max(10, 20)
Math.max(10, 20)

注意:仅仅包含逗号的函数参数定义或者函数调用会抛出 SyntaxError。而且,当使用剩余参数的时候,并不支持尾后逗号:

js
function f(,) {} // SyntaxError: missing formal parameter
(,) => {};       // SyntaxError: expected expression, got ','
f(,)             // SyntaxError: expected expression, got ','

function f(...p,) {} // SyntaxError: parameter after rest parameter
(...p,) => {}        // SyntaxError: expected closing parenthesis, got ','

Async/Await

js
function resolveAfter2Seconds(x) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve(x)
    }, 2000)
  })
}

// 赋值给变量的异步函数表达式
const add = async function (x) {
  const a = await resolveAfter2Seconds(20)
  const b = await resolveAfter2Seconds(30)
  return x + a + b
}

add(10).then((v) => {
  console.log(v) // 4 秒后打印 60
})

// 用作 IIFE 的异步函数表达式
;(async function (x) {
  const p1 = resolveAfter2Seconds(20)
  const p2 = resolveAfter2Seconds(30)
  return x + (await p1) + (await p2)
})(10).then((v) => {
  console.log(v) // 2 秒后打印 60
})

共享内存和原子

Atomics 命名空间对象包含对 SharedArrayBufferArrayBuffer 对象执行原子操作的静态方法。

与一般的全局对象不同,Atomics 不是构造函数。因此你不能将其与 new 运算符一起使用或将 Atomics 对象作为一个函数来进行调用。Atomics 的所有属性和方法都是静态的(与 Math 对象一样)。

原子操作

当内存被共享时,多个的线程能够读写内存上的同一数据。原子操作会确保正在读或写的数据的值是符合预期的,即下一个原子操作一定会在上一个原子操作结束后才会开始,其操作不会被中断。

等待和通知

wait()notify() 方法采用的是 Linux 上的 futex 模型(“快速用户空间互斥体”),可以让进程一直等待直到某个特定的条件为真,主要用于实现阻塞。