<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>
<!-- Mirrored from www.w3schools.com/js/tryit.asp?filename=tryjs_classes_getters by HTTrack Website Copier/3.x [XR&CO'2014], Wed, 21 Dec 2022 17:06:53 GMT -->
</html>