waltermilner

Untitled

Sep 20th, 2020
752
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function MyObject() {
  2.  
  3.             Object.defineProperty(this, 'accessA', {
  4.                 set(x) {
  5.                     if (x>0) this.a = x; // or whatever validation you want
  6.                 },
  7.                 get() { return this.a; }
  8.  
  9.             })
  10.  
  11.  
  12.             }
  13.  
  14.         var obj1 = new MyObject();
  15.          
  16.         obj1.accessA=77; // call the setter
  17.         console.log(obj1.accessA); // call the getter
RAW Paste Data