学习热乎的ES13

120 阅读2分钟

ECMAScript 2022 已完成的提案

calss类先相关

我们之前定义类的属性都是在constructor中绑定在this上的。例如:

class Person {
    constructor() {
        // public 属性                         
        this.name = "二狗子"              
        // private 属性                      
        this._gender = "未知"
    }                                      
    getName(){                          
        return this.name 
    }                                       
}

_gender这个属性,我们是期望定义为私有属性的,只能从类方法的内部访问它,不允许在外部访问。但是我们现在无法保证这一点。

const gou = new Person()
gou.name // 二狗子
gou._gender // 未知

在ES13中我们可以更加方便的定义属性:

  1. 不用在constructor中定义属性
  2. 可以通过 # 去定义私有属性
class Person {
    // public 属性                         
    name = "二狗子"              
    // private 属性                      
    #gender = "未知"                                   
    getName(){                          
        return this.name 
    }                                       
}

const gou = new Person()
gou.name // 二狗子
gou._gender // SyntaxError cannot be accessed or modified from outside the class

那么问题来了,我们怎样知道这个类中是否有私有属性呢?

检查类中的私有属性

我们可以在类的内部定义一个静态方法用来检查私有属性。

class Person {
    // public 属性                         
    name = "二狗子"              
    // private 属性                      
    #gender = "未知"                                   
    getName(){                          
        return this.name 
    }   
    static checkPrivate(o) {
        return #gender in o
    }
}

const gou = new Person()
Person.checkPrivate(gou) // true

还有一个问题,假如有两个人都叫二狗子,他们的检查方法可以通用吗?

class Person {                       
    #name = "二狗子"                                               
    getName(){                          
        return this.name 
    }   
    static checkPrivate(o) {
        return #name in o
    }
}

class People {                         
    #name = "二狗子"                                               
    getName(){                          
        return this.name 
    }   
    static checkPrivate(o) {
        return #name in o
    }
}


const gou = new Person()
const gou2 = new People()
Person.checkPrivate(gou) // true
Person.checkPrivate(gou2) // false

People.checkPrivate(gou) // false
People.checkPrivate(gou2) // true

由此,我们可以看出只能检查自己实例的私有属性。 那么如何从类外部授予对类的私有属性的访问权?

let getPrivate = null
class Person {                       
    #name;
    constructor(val) {
        this.#name = val
    }
    // 静态块,当类被创建的时候就会执行
    static {
        getPrivate = (o) => o.#name
    }
}

getPrivate(new Person('二狗子'))

正则

之前正则匹配字符串,不知道结束的下标,我们现在可以添加d字符并查看结果。 ES13之前:

const people = 'people: zsas, lslsls, ergouzi'
const regex = /(zsas)/g;
const matchObj = regex.exec(people);
console.log(matchObj);

结果如下图:

image.png 上图中index是匹配字符串的开始的索引。 在ES13中新增了d修饰符。

const people = 'people: zsas, lslsls, ergouzi'
const regex = /(zsas)/gd; // 这里加了一个d修饰符
const matchObj = regex.exec(people);
console.log(matchObj);

结果如下图:

image.png

await可以放在顶层

ES13之前await只能和async配合使用,现在可以在异步方法的上下文中使用,不再依赖async.

const a = await import('./a.js')

const res = await Promise.all([])

.at() 方法

该方法用于获取是数组(类数组)或者字符串中的值,需要传入一个对应的索引值。

// 数组
const arr = [1, 2, 3]
arr.at(0) // 1
arr.at(1) // 2
arr.at(2) // 3
arr.at(-2) // 2

// 字符串
const str = '123'
str.at(0) // 1
str.at(1) // 2
str.at(2) // 3
str.at(-1) // 3

从后面查找数组

const arr = [{ val: 1 }, { val: 2 }, { val: 3 }, { val: 4 }]
arr.findLast(item => item % 2 === 0) // 4
arr.findLastIndex(item => item % 2 === 0) // 3