Skip to content

Instantly share code, notes, and snippets.

@bbqsrc
Created December 14, 2013 14:06
Show Gist options
  • Save bbqsrc/7959522 to your computer and use it in GitHub Desktop.
Save bbqsrc/7959522 to your computer and use it in GitHub Desktop.
createPocketPlane - a way to create getters/setters using an internal slot!
// Pocket Plane - 2013 (c) Brendan Molloy - CC0 License
function createPocketPlane(object, prop, fallback) {
Object.defineProperty(object, prop, {
get: pocketPlane,
set: pocketPlane,
configurable: true
});
function pocketPlane() {
var val;
Object.defineProperty(this, prop, {
set: magic,
get: magic
});
return magic.apply(this, arguments);
function magic() {
if (arguments.length > 0) {
val = arguments[0];
} else {
return val === undefined ? fallback : val;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment