Last active
November 13, 2018 18:18
-
-
Save hiddentao/5946053 to your computer and use it in GitHub Desktop.
Generate overridable getters and setters in Javascript
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
// see blog post: http://www.hiddentao.com/archives/2013/07/08/generate-overridable-getters-and-setters-in-javascript/ | |
Function.prototype.generateProperty = function(name, options) { | |
// internal member variable name | |
var privateName = '__' + name; | |
options = options || {}; | |
options.get = ('undefined' === typeof options.get ? true : options.get ); | |
options.set = ('undefined' === typeof options.set ? true : options.set ); | |
// pre-initialise the internal variable? | |
if (options.defaultValue) { | |
this.prototype[privateName] = options.defaultValue; | |
} | |
var definePropOptions = {}, | |
getterName = '__get_' + name, | |
setterName = '__set_' + name; | |
// generate the getter | |
if(true === options.get) { | |
this.prototype[getterName] = function() { | |
return this[privateName]; | |
}; | |
} | |
// use custom getter | |
else if (options.get) { | |
this.prototype[getterName] = options.get; | |
} | |
// disable getter | |
else { | |
this.prototype[getterName] = function() { | |
throw new Error('Cannot get: ' + name); | |
} | |
} | |
definePropOptions.get = function() { | |
return this[getterName].call(this); | |
}; | |
// generate the setter | |
if(true === options.set) { | |
this.prototype[setterName] = function(val) { | |
this[privateName] = val; | |
}; | |
} | |
// use custom setter | |
else if (options.set) { | |
this.prototype[setterName] = options.set; | |
} | |
// disable setter | |
else { | |
this.prototype[setterName] = function() { | |
throw new Error('Cannot set: ' + name) | |
}; | |
} | |
definePropOptions.set = function(val) { | |
this[setterName].call(this, val); | |
}; | |
// do it! | |
Object.defineProperty(this.prototype, name, definePropOptions); | |
}; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Override in subclass: