运行 ❯
获取您
自己的
网站
×
更改方向
保存代码
更改主题,深色/浅色
前往 Spaces
<!DOCTYPE html> <html> <body> <h1>JavaScript Objects</h1> <h2>The Object.defineProperty() Method</h2> <p>JavaScript Getters and Setters are 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>