Answers for "js object property getter setter"

0

js object property getter setter

const obj = {
  value: 0,
  get prop() {
    console.log(".prop accessed!");
    return this.value;
  },
  set prop(newValue) {
    console.log(".prop set! Value: ", newValue);
    this.value = newValue;
  }
}

console.log(obj.prop);
obj.prop = 42;
console.log(obj.prop);
Posted by: Guest on October-26-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language