getter and setter in JavaScript

August 16, 2015

You can define getter and setter functions in JS, which from the outside just look like normal objects properties, but since they're functions can do additional work when accessed.
Could for example be useful to log access to properties when you're not sure if they're still used somewhere.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get
 

Kind of reminds me of Ruby's Virtual Attributes.

Example getter and setter

var dog = { get age() { console.log('accessed age'); return this._age; }, set age(age) { console.log('set age'); this._age = age; }, }; dog.age = 8; console.log(dog.age);