运行 ❯
获取您
的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Object</h1> <h2>JavaScript Getters and Setters</h2> <p>Perfect for creating counters:</p> <p id="demo"></p> <script> // Define an Object const obj = {counter:0}; // Define Setters and Getters Object.defineProperty(obj, "reset", { get : function () {this.counter = 0;} }); Object.defineProperty(obj, "increment", { get : function () {this.counter++;} }); Object.defineProperty(obj, "decrement", { get : function () {this.counter--;} }); Object.defineProperty(obj, "add", { set : function (value) {this.counter += value;} }); Object.defineProperty(obj, "subtract", { set : function (value) {this.counter -= value;} }); // Play with counter: obj.reset; obj.add = 5; obj.subtract = 1; obj.increment; obj.decrement; document.getElementById("demo").innerHTML = obj.counter; </script> </body> </html>