Skip to content

Instantly share code, notes, and snippets.

@codebubb
Last active December 16, 2019 14:27
Show Gist options
  • Select an option

  • Save codebubb/8a8fb073d78c7ed281a4d0cbaafb5eb6 to your computer and use it in GitHub Desktop.

Select an option

Save codebubb/8a8fb073d78c7ed281a4d0cbaafb5eb6 to your computer and use it in GitHub Desktop.
Proxy Example
const userProxy = {
get: function (target, name) {
return name === 'password' ? '*'.repeat(target.password.length) : target[name];
}
}
// or shorter
const userProxy = {
get: (target, name) => name === 'password' ? '*'.repeat(target.password.length) : target[name],
}
// With set as well as get
const userProxy = {
get: (target, name) => name === 'password' ? '*'.repeat(target.password.length) : target[name],
set: (object, property, value) => {
if (property === 'password' && value.length < 8) {
throw new RangeError('Passwords must be at least 8 characters long');
}
object[property] = value;
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment