ES13新特性
1、类新增特性
1.1、私有属性和方法
我们还可以给类定义静态成员和静态私有函数。类的静态方法可以使用this关键字访问其他的私有或者公有的普通成员。
//加#号代表私有属性或私有方法
class Cache {
static #count = 0
static getCount() {
return this.#count
}
#obj = {
}
get(key) {
return this.#obj[key]
}
set(key, value) {
this.#obj[key] = value
}
#prstore() {
}
has() {
//判断#obj是不是当前类的属性
return #obj in this
}
}
let store = new Cache()
store.set("name", "twj")
store.set("age", 100)
console.log(store)
console.log(store.get("name")) //twj
//console.log(store.#obj) //直接报错
//console.log(Cac


193

被折叠的 条评论
为什么被折叠?



