Skip to content

Instantly share code, notes, and snippets.

@tjjfvi
Last active November 2, 2018 19:52
Show Gist options
  • Save tjjfvi/440e9705cda4a9380efd1093e87411bf to your computer and use it in GitHub Desktop.
Save tjjfvi/440e9705cda4a9380efd1093e87411bf to your computer and use it in GitHub Desktop.
Function to create a Proxy to gnerate setters on an object
generateSetters = (obj) =>
new Proxy({}, {
get: (_, name) => {
name =
name.startsWith("_") ?
name.slice(1) :
name.startsWith("set") ?
name[3].toLowerCase() + name.slice(4) :
name
;
return val =>
obj[name] = val
}
});
/* Usage:
let {
setProp1,
_prop2,
prop3
} = generateSetters(obj);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment