其他分享
首页 > 其他分享> > 29.class中的getter与setter

29.class中的getter与setter

作者:互联网

<!DOCTYPE html> <html lang="en">   <head>     <meta charset="UTF-8" />     <meta http-equiv="X-UA-Compatible" content="IE=edge" />     <meta name="viewport" content="width=device-width, initial-scale=1.0" />     <title>Document</title>   </head>   <body>     <script>       // get和set       class Phone {         // class里面可以没有构造函数         get price() {           //读取price会调用set,里面的返回值,就是price属性的值           console.log("价格属性被读取了");           return 123;         }
        set price(value) {           // 对price设置的时候,会调用set,里面必须要接收一个形参value           console.log("价格属性被修改了", value);         }       }
      let s = new Phone();       s.price = 100;       console.log(s.price);     </script>   </body> </html>

标签:set,console,log,price,29,value,getter,class,setter
来源: https://www.cnblogs.com/renag/p/15757700.html