DEV Community

Discussion on: Explain Calling And Setting Getters And Setters Like I'm Five

Collapse
 
alexgwartney profile image
Alex Gwartney

So first I wanted to say thanks for the detailed reply. I have to head off to work so am leaving a short response but when I get the chance to I will respond with more info. But I did want to quickly respond and say that I was able to figure out the answer. I think pretty much everything I describe below is a general overview of what you have already said but I figured I would also put what I found. Which will hopefully help others out.

Essentially what I figured out was that before get and set keywords were implemented. You used to have to define the get and set methods through the Object.define property method.
Which explains as to why we can call the get and set methods the way we do. Because they are actually just properties.I posted an example below that I got from the mdn docs.


var o = {}

Object.defineProperty(o, 'b', {
  // Using shorthand method names (ES2015 feature).
  // This is equivalent to:
  // get: function() { return bValue; },
  // set: function(newValue) { bValue = newValue; },
  get() { return bValue; },
  set(newValue) { bValue = newValue; },
  enumerable: true,
  configurable: true
});

o.b; // 38