crop_rotate
Run
<!DOCTYPE html> <html> <body> <h2>JavaScript Class Gettter/Setter</h2> <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; } } let myCar = new Car("Ford"); document.getElementById("demo").innerHTML = myCar.cnam; </script> </body> </html>
PCFET0NUWVBFIGh0bWw+DQo8aHRtbD4NCjxib2R5Pg0KDQo8aDI+SmF2YVNjcmlwdCBDbGFzcyBHZXR0dGVyL1NldHRlcjwvaDI+DQoNCjxwPkEgZGVtb25zdHJhdGlvbiBvZiBob3cgdG8gYWRkIGdldHRlcnMgYW5kIHNldHRlcnMgaW4gYSBjbGFzcywgYW5kIGhvdyB0byB1c2UgdGhlIGdldHRlciB0byBnZXQgdGhlIHByb3BlcnR5IHZhbHVlLjwvcD4NCg0KPHAgaWQ9ImRlbW8iPjwvcD4NCg0KPHNjcmlwdD4NCmNsYXNzIENhciB7DQogIGNvbnN0cnVjdG9yKGJyYW5kKSB7DQogICAgdGhpcy5jYXJuYW1lID0gYnJhbmQ7DQogIH0NCiAgZ2V0IGNuYW0oKSB7DQogICAgcmV0dXJuIHRoaXMuY2FybmFtZTsNCiAgfQ0KICBzZXQgY25hbSh4KSB7DQogICAgdGhpcy5jYXJuYW1lID0geDsNCiAgfQ0KfQ0KDQpsZXQgbXlDYXIgPSBuZXcgQ2FyKCJGb3JkIik7DQoNCmRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCJkZW1vIikuaW5uZXJIVE1MID0gbXlDYXIuY25hbTsNCjwvc2NyaXB0Pg0KDQo8L2JvZHk+DQo8L2h0bWw+