Skip to content

Instantly share code, notes, and snippets.

@Frost
Created July 30, 2014 18:04
Show Gist options
  • Select an option

  • Save Frost/a307b68e51ce9f6e7de0 to your computer and use it in GitHub Desktop.

Select an option

Save Frost/a307b68e51ce9f6e7de0 to your computer and use it in GitHub Desktop.
ECMAScript 6 Proxy Object with default property value

Don't even ask...

It seemed like a cool idea. I am not actually using this for anything.

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;
}
}
});
}
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