Skip to content

Instantly share code, notes, and snippets.

@sbin0819
Created April 17, 2023 05:22
Show Gist options
  • Save sbin0819/70d1c93b8ea64528ecf7f435776f0c9b to your computer and use it in GitHub Desktop.
Save sbin0819/70d1c93b8ea64528ecf7f435776f0c9b to your computer and use it in GitHub Desktop.
const personProxy = new Proxy(person, {
  get: (obj, prop) => {
    if (!obj[prop]) {
      console.log(
        `Hmm.. this property doesn't seem to exist on the target object`
      );
    } else {
      console.log(`The value of ${prop} is ${obj[prop]}`);
    }
  },
  set: (obj, prop, value) => {
    if (prop === "age" && typeof value !== "number") {
      console.log(`Sorry, you can only pass numeric values for age.`);
    } else if (prop === "name" && value.length < 2) {
      console.log(`You need to provide a valid name.`);
    } else {
      console.log(`Changed ${prop} from ${obj[prop]} to ${value}.`);
      obj[prop] = value;
    }
  },
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment