Created
December 14, 2013 14:06
-
-
Save bbqsrc/7959522 to your computer and use it in GitHub Desktop.
createPocketPlane - a way to create getters/setters using an internal slot!
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
// 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