gpt4 book ai didi

javascript - 如何在类 ES6 中创建私有(private)变量和方法?

转载 作者:行者123 更新时间:2023-11-30 00:25:38 25 4
gpt4 key购买 nike

<分区>

如何使用 ES6 类创建应由同一类的公共(public)方法访问的私有(private)变量和方法。

class MyClass {
constructor() {
this.publicVar = "I am public";
//some private Variable e.g privteVar1 = "I am Private1"; privateVar2 = "I am Private2";
this.publicMethod = () => {
//it should have accesses to private variables and methods
console.log(privateVar1, privateVar2)
};
//similarly need to create some privateMethod
}

render() {
//it also should have access to the private variables
}

}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com