运行 ❯
获取您
的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Class Getter/Setter</h1> <p>A demonstration of how to add getters and setters in a class, and how to use the getter to get the property value.</p> <p id="demo"></p> <script> class Car { constructor(brand) { this.carname = brand; } get cnam() { return this.carname; } set cnam(x) { this.carname = x; } } const myCar = new Car("Ford"); document.getElementById("demo").innerHTML = myCar.cnam; </script> </body> </html>