Announcement

Collapse
No announcement yet.

Can I add getters and setters using defineProperty method

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can I add getters and setters using defineProperty method

    Can I add getters and setters using defineProperty method



    Yes, You can use the Object.defineProperty() method to add Getters and Setters. For example, the below counter object uses increment, decrement, add and subtract properties,


    Code:
    var obj = {counter : 0};
    
    // Define getters
    Object.defineProperty(obj, "increment", {
    get : function () {this.counter++;}
    });
    Object.defineProperty(obj, "decrement", {
    get : function () {this.counter--;}
    });
    
    // Define setters
    Object.defineProperty(obj, "add", {
    set : function (value) {this.counter += value;}
    });
    Object.defineProperty(obj, "subtract", {
    set : function (value) {this.counter -= value;}
    });
    
    obj.add = 10;
    obj.subtract = 5;
    console.log(obj.increment); //6
    console.log(obj.decrement); //5

Related Topics

Collapse

Working...
X