ember like getter setter

for education purposes getter setter computed prop with caching

by aniketmhatre88

Babel + JSX

let obj = {
  set(prop, val) {
    if (typeof val === 'function') {
      let value;
      this[prop] = function(...args) {
        if (value) {
          return value;
        }
        value = val.apply(this, args);
        return value;
      }
    } else {
			this[prop] = val;
    }
  },
  get(prop) {
    let val = this[prop];
    if(typeof val === 'function') {
      return val.call(this);
    }
    return val;
  }
};

obj.set('foo', function() {return 5;});
console.log(obj.get('foo'));
console.log(obj.get('foo'));