It seemed like a cool idea. I am not actually using this for anything.
Created
July 30, 2014 18:04
-
-
Save Frost/a307b68e51ce9f6e7de0 to your computer and use it in GitHub Desktop.
ECMAScript 6 Proxy Object with default property 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
| 1 | |
| 17 | |
| 3 | |
| 17 | |
| 17 |
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
| module.exports = exports = ObjectWithDefaultProperty; | |
| function ObjectWithDefaultProperty(properties, defaultValue) { | |
| return Proxy.create({ | |
| get: function get(proxy, name) { | |
| if (name in properties) { | |
| return properties[name]; | |
| } else { | |
| return defaultValue; | |
| } | |
| } | |
| }); | |
| } |
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
| var ObjectWithDefaultProperty = require('./ObjectWithDefaultProperty'); | |
| var properties = {a: 1, c: 3}, | |
| defaultValue = 17, | |
| proxy = new ObjectWithDefaultProperty(properties, defaultValue); | |
| console.log(proxy.a); | |
| console.log(proxy.b); | |
| console.log(proxy.c); | |
| console.log(proxy.d); | |
| console.log(proxy.e); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment