Skip to content

Instantly share code, notes, and snippets.

@MiguelSavignano
Created May 6, 2019 13:47
Show Gist options
  • Save MiguelSavignano/80b9ab7829eecf9647fa35f15189a3f2 to your computer and use it in GitHub Desktop.
Save MiguelSavignano/80b9ab7829eecf9647fa35f15189a3f2 to your computer and use it in GitHub Desktop.
Decorate class property with ensure default value, boolean can't be null.
const DefaultValue = defaultValue =>
function(target, key) {
let _val = this[key];
// delete old property
if (delete this[key]) {
Object.defineProperty(target, key, {
get: function() {
return !!_val ? _val : defaultValue;
},
set: function(newVal) {
return (_val = newVal);
},
enumerable: true,
configurable: true,
});
}
};
module.exports.DefaultValue = DefaultValue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment