Created
June 25, 2015 15:22
-
-
Save cdelaorden/f36372fca9ed024f9f44 to your computer and use it in GitHub Desktop.
ES2015 - Proxy - default value
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
//Default values | |
let defaultValuesHandler = { | |
get(target,name) { | |
return name in target ? target[name] : 'My default value' | |
} | |
}; | |
let target = { | |
name: 'Carlos', | |
favGame: 'Mass Effect' | |
}; | |
let proxy = new Proxy(target, defaultValuesHandler); | |
console.log(proxy.name); | |
console.log(proxy.favGame); | |
//non existing property, return default value | |
console.log(proxy.age); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As of today (25 Jun 2015) only works in latest Firefox / Microsoft Edge