• 5 个最令人兴奋的 ES13 功能


    ES13 (ECMAScript 2022) 已经发布很久了,并且更新了许多有用的功能。

    在这篇文章中,我想与大家分享 5 种我最喜欢的技术,这些技术是我已经开始在工作中实施的。

    1. 顶级await

    await 是我最喜欢的功能,因为它使我的代码显得更加优雅。确实,不再有回调地狱的负担。

    1. // Old Style
    2. const getUserInfoOld = () => {
    3. return fetch('/getUserId')
    4. .then((userId) => {
    5. return fetch('/getUserInfo', { body: JSON.stringify({ userId }) })
    6. .then((userInfo) => {
    7. return userInfo
    8. })
    9. })
    10. }
    11. // await Style
    12. const getUserInfo = async () => {
    13. const userId = await fetch('/getUserId')
    14. const userInfo = await fetch('/getUserInfo', {
    15. body: JSON.stringify({ userId })
    16. })
    17. return userInfo
    18. }

    “async”函数并不是唯一可以使用“await”的地方;你甚至可以这样使用它,这真是太神奇了。

    1. const mockUserInfo = () => {
    2. return new Promise((resolve) => {
    3. setTimeout(() => {
    4. resolve({
    5. userName: 'fatfish'
    6. })
    7. }, 1000)
    8. })
    9. }
    10. const userInfo = await mockUserInfo()
    11. // Can we print out { userName: 'fatfish' } please?
    12. console.log(userInfo)

    这个功能非常棒,可以让我们做很多以前做不到的事情。

    1.动态加载模块

    const enStrings = await import(`/i18n/en`)

    2.初始化数据库

    const connection = await connectDB()

    3. 有条件渲染模块

    1. const showBlackTheme = window.location.search.includes('theme=black')
    2. if (showBlackTheme) {
    3. await import('/theme/black.js')
    4. } else {
    5. await import('/theme/white.js')
    6. }

    2. Object.hasOwn

    我们经常需要知道对象上是否存在某个属性。怎么做?

    “in”或“obj.hasOwnProperty”是用于此目的的两种最常用的方法。

    但它们都有一些缺陷,让我们来看看。

    “in”运算符

    如果指定的属性位于指定的对象或其原型链中,则 in 运算符返回 true。

    1. const Person = function (age) {
    2. this.age = age
    3. }
    4. Person.prototype.name = 'fatfish'
    5. const p1 = new Person(24)
    6. console.log('age' in p1) // true
    7. console.log('name' in p1) // true pay attention here

    obj.hasOwnProperty

    hasOwnProperty 方法返回一个布尔值,指示对象是否将指定属性作为其自己的属性(而不是继承它)。

    使用上面相同的例子

    1. const Person = function (age) {
    2. this.age = age
    3. }
    4. Person.prototype.name = 'fatfish'
    5. const p1 = new Person(24)
    6. console.log(p1.hasOwnProperty('age')) // true
    7. console.log(p1.hasOwnProperty('name')) // fasle pay attention here

    也许“obj.hasOwnProperty”已经可以过滤掉原型链上的属性,但在某些情况下,它并不安全,会导致程序失败。

    1. Object.create(null).hasOwnProperty('name')
    2. // Uncaught TypeError: Object.create(...).hasOwnProperty is not a function

    Object.hasOwn

    不用担心,我们可以使用“Object.hasOwn”来规避这两个问题,这比“obj.hasOwnProperty”方法更方便、更安全。

    1. let object = { age: 24 }
    2. Object.hasOwn(object, 'age') // true
    3. let object2 = Object.create({ age: 24 })
    4. Object.hasOwn(object2, 'age') // false The 'age' attribute exists on the prototype
    5. let object3 = Object.create(null)
    6. Object.hasOwn(object3, 'age') // false an object that does not inherit from "Object.prototype"

    3. 数组“.at()”方法

    我们应该如何从数组的末尾开始获取其中的元素呢?

    是的,我们需要以“array.length — 1”作为下标来读取。

    1. const array = [ 1, 2, 3, 4, 5 ]
    2. const lastEle = array[ array.length - 1 ] // 5
    3. // You can't read like that
    4. const lastEle = array[ - 1 ] // undefined

    还有其他办法吗?

    ES2022提供了一个名为at的数组方法,这可能是一个很小的改变,但是可以大大提高代码的可读性。

    at 方法可以取正数或负数,这将决定它是从数组的头部还是尾部开始读取元素。

    1. const array = [ 1, 2, 3, 4, 5 ]
    2. const lastEle = array.at(-1) // 5
    3. const firstEle = array.at(0) // 1

    4. Error cause

    在ES2022规范中,new Error()可以指定其错误的原因。

    1. const readFiles = (filePaths) => {
    2. return filePaths.map(
    3. (filePath) => {
    4. try {
    5. // ···
    6. } catch (error) {
    7. throw new Error(
    8. `${filePath} erroe`,
    9. {cause: error}
    10. )
    11. }
    12. })
    13. }

    有时,代码块的错误需要根据其原因进行不同的处理,但错误的原因相对相似,因此,能够为它们分配错误名称是很棒的。

    5. 私有槽位及方法

    过去,我们使用“_”来表示私有属性,但它并不安全,仍然可能被外部修改。

    1. class Person {
    2. constructor (name) {
    3. this._money = 1
    4. this.name = name
    5. }
    6. get money () {
    7. return this._money
    8. }
    9. set money (money) {
    10. this._money = money
    11. }
    12. showMoney () {
    13. console.log(this._money)
    14. }
    15. }
    16. const p1 = new Person('fatfish')
    17. console.log(p1.money) // 1
    18. console.log(p1._money) // 1
    19. p1._money = 2 // Modify private property _money from outside
    20. console.log(p1.money) // 2
    21. console.log(p1._money) // 2

    我们可以使用“#”来实现真正安全的私有属性

    1. class Person {
    2. #money=1
    3. constructor (name) {
    4. this.name = name
    5. }
    6. get money () {
    7. return this.#money
    8. }
    9. set money (money) {
    10. this.#money = money
    11. }
    12. showMoney () {
    13. console.log(this.#money)
    14. }
    15. }
    16. const p1 = new Person('fatfish')
    17. console.log(p1.money) // 1
    18. // p1.#money = 2 // We cannot modify #money in this way
    19. p1.money = 2
    20. console.log(p1.money) // 2
    21. console.log(p1.#money) // Uncaught SyntaxError: Private field '#money' must be declared in an enclosin
  • 相关阅读:
    Vue脚手架搭建
    <C++>解密 构造函数和析构函数
    Linux安装部署oracle数据库
    Hbase工作原理
    nginx的正向代理、反向代理、负载均衡
    【学校的位置|新学校选址】python实现-附ChatGPT解析
    开发 Chrome 扩展 之 Hello World 心血来潮
    MySQL数据库介绍
    多线程 - 阻塞式队列
    Spring异步任务async介绍与案例实战
  • 原文地址:https://blog.csdn.net/sdasadasds/article/details/132981066