Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const birthDateHandler = {
- get: (target, property) => {
- if (property === "birthDate"){
- const currentYear = new Date().getFullYear()
- return currentYear - target.age
- }
- const propertyExists = property in target
- return propertyExists
- ? target[property]
- : new Error(`the property ${property} does not exist!`)
- }
- }
- user = new Proxy(user, birthDateHandler)
- console.log(user.birthDate)
- //output: `${user.birthDate}`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement