Created
June 25, 2015 15:29
-
-
Save cdelaorden/fa26759de5366e80dec8 to your computer and use it in GitHub Desktop.
ES2015 - Proxy - Property set validation
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
//Validation Proxy example | |
let handler = { | |
//Intercept any object property set operation | |
set: function(obj, prop, value){ | |
if(prop === 'age' && !Number.isInteger(value)){ | |
throw new TypeError('Age should be an integer number'); | |
} | |
obj[prop] = value; | |
} | |
} | |
let target = { | |
age: 25 | |
}; | |
let proxy = new Proxy(target, handler); | |
proxy.age = 'foo'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment