Created
November 13, 2012 21:12
-
-
Save seejohnrun/4068434 to your computer and use it in GitHub Desktop.
reverting defaults
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
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