运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Class Setters</h1> <p>When using a setter to set a property value, you do not use parantheses.</p> <p id="demo"></p> <script> class Car { constructor(brand) { this._carname = brand; } set carname(x) { this._carname = x; } get carname() { return this._carname; } } const myCar = new Car("Ford"); myCar.carname = "Volvo"; document.getElementById("demo").innerHTML = myCar.carname; </script> </body> </html>