Last active
December 16, 2019 14:27
-
-
Save codebubb/8a8fb073d78c7ed281a4d0cbaafb5eb6 to your computer and use it in GitHub Desktop.
Proxy Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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