Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Class Getter/Setter

is there a way to listen for a property call on a JavaScript Class

for example when i go something like this:

myForm =  new Form();

myForm.name = 'Name'; 

-> when i set the name i dont only want to set the property but i also want to update my Vuex store. Same thing with get i would like to read from Vuex store.

I knoew there are thins like Proxy but for this i need to wrap my Class with a Proxy object. Not so sure if i like this.

module.exports = new Proxy(new Form({}), {
    get (receiver, name) {
        console.log('getting property from Vuex Store');
    }
});

What i need is something like this:

module.exports = class Form {

//this should be triggered when form.something
get(property) {
return this[property];
}

//this should be triggered when from.something = 'something'
set(property, value) { 
return this[property] = value;
}
};

it there a best practice for this?

like image 460
therabbityouknow Avatar asked Apr 18 '18 08:04

therabbityouknow


People also ask

What is getter and setter in class JavaScript?

In a JavaScript class, getters and setters are used to get or set the properties values. “get” is the keyword utilized to define a getter method for retrieving the property value, whereas “set” defines a setter method for changing the value of a specific property.

Do JavaScript have getters setters?

JavaScript Accessors (Getters and Setters) ECMAScript 5 (ES5 2009) introduced Getter and Setters. Getters and setters allow you to define Object Accessors (Computed Properties).

Do JavaScript classes need getters?

Classes allow using getters and setters. It is smart to use getters and setters for the properties, especially if you want to do something special with the value before returning them, or before you set them. To add getters and setters in the class, use the get and set keywords.

What is the difference between getter and setter in JavaScript?

Getters and setters allow us to define Object Accessors. The difference between them is that the former is used to get the property from the object whereas the latter is used to set a property in an object.

How to add getters and setters in JavaScript classes?

getters and setters in JavaScript classes? Classes allow using getters and setters. It is smart to use getters and setters for the properties, especially if you want to do something special with the value before returning them, or before you set them. To add getters and setters in the class, use the get and set keywords.

How to get the property value of a getter and setter?

In the following example, getters and setters were added to the class ' Company ' and the property value is obtained by using a ' get ' keyword.

Can a getter have a value in JavaScript?

Description. In JavaScript, this can be accomplished with the use of a getter. It is not possible to simultaneously have a getter bound to a property and have that property actually hold a value, although it is possible to use a getter and a setter in conjunction to create a type of pseudo-property.

What is the use of setter in JavaScript?

JavaScript Setter In JavaScript, setter methods are used to change the values of an object.


1 Answers

Javascript supports getters and setters

class Form{
  set foo(val){
    console.log("setting foo")
    this.fooValue = val;
  }
  
  get foo(){
     console.log("getting foo");
     return this.fooValue;
  }
}

let frm = new Form();
frm.foo = "bar";
console.log(frm.foo);

You could make this more dynamic by writing a withGetterSetter method which wraps each property of an object with a getter/setter.

var form = {
  a: "aValue",
  b: "bValue"
}

function withGetterSetter(obj){
   var keys = Object.keys(obj);
   var result = {};
   
   for(var i=0;i<keys.length;i++){
       var key = keys[i];
       result[key+"_internal"] = obj[key];
       (function(k){
         Object.defineProperty(result,k, {
          get:function() {
            console.log("getting property:",k);
            return this[k + "_internal"];
          }, 
          set: function(x) { 
            console.log("setting property:",k);
            this[k + "_internal"] = x 
          }
         });
       })(key)
   }
   return result;
}

var setterObj = withGetterSetter(form);
console.log(setterObj.a);
setterObj.a = "updated";
console.log(setterObj.a);

It works by copying each property p to a new object with p_internal and creating a dynamic get/set for the original property name.

like image 87
Jamiec Avatar answered Oct 11 '22 10:10

Jamiec