Skip to content

Instantly share code, notes, and snippets.

@seejohnrun
Created November 13, 2012 21:12
Show Gist options
  • Save seejohnrun/4068434 to your computer and use it in GitHub Desktop.
Save seejohnrun/4068434 to your computer and use it in GitHub Desktop.
reverting defaults
var settings = Object.create({
_defaults: {},
_values: {},
setDefault: function (key, value) {
this._values[key] = this[key]
this._defaults[key] = value
Object.defineProperty(this, key, {
get: function () {
var undef = typeof this._values[key] === 'undefined'
return undef ? this._defaults[key] : this._values[key]
},
set: function (value) {
this._values[key] = value
}
});
}
});
module.exports = {
create: function () {
return Object.create(settings)
}
}
///
var obj = settings.create()
obj.hello // obj === undefined
obj.hello = 5 // obj === 5
obj.setDefault('hello', 1); // obj === 5
obj.hello = undefined // obj === 1
obj.hello = null // obj === null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment