ECMAScript 7、8、9新特性

143 阅读3分钟

ECMAScript 7、8、9新特性

ECMAScript 每年都会发布新版本,ES7(2016)、ES8(2017)和 ES9(2018)分别引入了一些新特性。以下是这些版本的主要更新内容:

ECMAScript 2016(ES7)

ES7 是一个较小的更新,只引入了两个新特性。

  1. Array.prototype.includes()

判断数组是否包含某个值,返回布尔值。

示例:

const arr = [1, 2, 3];
console.log(arr.includes(2)); // 输出: true
console.log(arr.includes(4)); // 输出: false
  1. 指数运算符 (**)

用于计算幂运算。

示例:

console.log(2 ** 3); // 输出: 8
console.log(3 ** 2); // 输出: 9

ECMAScript 2017(ES8)

ES8 引入了更多实用的特性,主要集中在异步编程和对象操作上。

  1. async/await

简化异步操作,使异步代码看起来像同步代码。

示例:

async function fetchData() {
    const response = await fetch('https://api.example.com/data');
    const data = await response.json();
    return data;
}
  1. Object.values()

返回对象所有可枚举属性值的数组。

示例:

const obj = { a: 1, b: 2, c: 3 };
console.log(Object.values(obj)); // 输出: [1, 2, 3]
  1. Object.entries()

返回对象所有可枚举属性的键值对数组。

示例:

const obj = { a: 1, b: 2, c: 3 };
console.log(Object.entries(obj)); // 输出: [['a', 1], ['b', 2], ['c', 3]]
  1. Object.getOwnPropertyDescriptors()

返回对象所有自身属性的描述符(包括 valuewritableenumerableconfigurable)。

示例:

const obj = { a: 1 };
const descriptors = Object.getOwnPropertyDescriptors(obj);
console.log(descriptors);
// 输出:
// {
//   a: { value: 1, writable: true, enumerable: true, configurable: true }
// }
  1. 字符串填充方法:padStart()padEnd()

在字符串的开头或结尾填充指定字符,直到字符串达到指定长度。

示例:

console.log('5'.padStart(3, '0')); // 输出: "005"
console.log('5'.padEnd(3, '*'));   // 输出: "5**"
  1. 函数参数列表和调用中的尾逗号

允许在函数参数列表和调用时使用尾逗号。

示例:

function foo(a, b, c,) {
    console.log(a, b, c);
}
foo(1, 2, 3,);

ECMAScript 2018(ES9)

ES9 进一步增强了异步编程和对象操作的能力。

  1. 异步迭代器 (for-await-of)

用于遍历异步可迭代对象(如异步生成器)。

示例:

async function* asyncGenerator() {
    yield 1;
    yield 2;
    yield 3;
}

(async () => {
    for await (const value of asyncGenerator()) {
        console.log(value);
    }
})();
// 输出: 1, 2, 3
  1. Promise.prototype.finally()

无论 Promise 是成功还是失败,都会执行的回调函数。

示例:

fetch('https://api.example.com/data')
    .then(response => response.json())
    .catch(error => console.error(error))
    .finally(() => console.log('请求完成'));
  1. Rest/Spread 属性

允许在对象中使用 ... 进行剩余参数和展开操作。

示例:

// Rest 属性
const { a, ...rest } = { a: 1, b: 2, c: 3 };
console.log(rest); // 输出: { b: 2, c: 3 }

// Spread 属性
const obj1 = { a: 1, b: 2 };
const obj2 = { ...obj1, c: 3 };
console.log(obj2); // 输出: { a: 1, b: 2, c: 3 }
  1. 正则表达式的增强
  • 命名捕获组:使用 (?<name>...) 语法为捕获组命名。
  • 反向断言:支持 (?<=...)(?<!...) 语法。
  • s 标志:使 . 匹配任意字符,包括换行符。

示例:

// 命名捕获组
const regex = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const match = regex.exec('2023-10-05');
console.log(match.groups.year);  // 输出: 2023
console.log(match.groups.month); // 输出: 10

// s 标志
const regex2 = /a.b/s;
console.log(regex2.test('a\nb')); // 输出: true

总结

  • ES7(2016)Array.prototype.includes() 和指数运算符 (**)。
  • ES8(2017)async/awaitObject.values()Object.entries()padStart()padEnd() 等。
  • ES9(2018):异步迭代器、Promise.prototype.finally()、Rest/Spread 属性、正则表达式增强等。

这些新特性使得 JavaScript 更加现代化和强大,尤其是在异步编程和对象操作方面。

更多vue相关插件及后台管理模板可访问vue admin reference,代码详情请访问github