crop_rotate
Run
<!DOCTYPE html> <html> <body> <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>
PCFET0NUWVBFIGh0bWw+DQo8aHRtbD4NCjxib2R5Pg0KDQo8aDI+SmF2YVNjcmlwdCBHZXR0ZXJzIGFuZCBTZXR0ZXJzPC9oMj4NCg0KPHA+UGVyZmVjdCBmb3IgY3JlYXRpbmcgY291bnRlcnM6PC9wPg0KDQo8cCBpZD0iZGVtbyI+PC9wPg0KDQo8c2NyaXB0Pg0KLy8gRGVmaW5lIGFuIG9iamVjdA0KY29uc3Qgb2JqID0ge2NvdW50ZXIgOiAwfTsNCg0KLy8gRGVmaW5lIFNldHRlcnMgYW5kIEdldHRlcnMNCk9iamVjdC5kZWZpbmVQcm9wZXJ0eShvYmosICJyZXNldCIsIHsNCiAgZ2V0IDogZnVuY3Rpb24gKCkge3RoaXMuY291bnRlciA9IDA7fQ0KfSk7DQpPYmplY3QuZGVmaW5lUHJvcGVydHkob2JqLCAiaW5jcmVtZW50Iiwgew0KICBnZXQgOiBmdW5jdGlvbiAoKSB7dGhpcy5jb3VudGVyKys7fQ0KfSk7DQpPYmplY3QuZGVmaW5lUHJvcGVydHkob2JqLCAiZGVjcmVtZW50Iiwgew0KICBnZXQgOiBmdW5jdGlvbiAoKSB7dGhpcy5jb3VudGVyLS07fQ0KfSk7DQpPYmplY3QuZGVmaW5lUHJvcGVydHkob2JqLCAiYWRkIiwgew0KICBz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