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