Created
May 6, 2019 13:47
-
-
Save MiguelSavignano/80b9ab7829eecf9647fa35f15189a3f2 to your computer and use it in GitHub Desktop.
Decorate class property with ensure default value, boolean can't be null.
This file contains 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
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