js类的get和set特性


 1         class ClassWithGetSet {
 2             #msg = 'hello world';
 3             get msg() {
 4                 // return this.#msg;
 5                 return this.#msg.replace(/w[a-z]+/,'jackal');
 6             }
 7             set msg(x) {
 8                 this.#msg = `hello ${x}`;
 9             }
10         }
11 
12         const instance = new ClassWithGetSet();
13         console.log(instance.msg);
14         // expected output: "hello jackal"
15 
16         instance.msg = 'cake';
17         console.log(instance.msg);
18         // 预期输出值: "hello cake"

这就class中的特性与Object.defineProperty类似


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM