Skip to content

Instantly share code, notes, and snippets.

@Narayon
Forked from simaovergalho/private-members.js
Last active April 8, 2018 01:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Narayon/ab1758e584bcab170d9e to your computer and use it in GitHub Desktop.
Save Narayon/ab1758e584bcab170d9e to your computer and use it in GitHub Desktop.
Javascript: Embed Private Members Into an Object
//reference to: http://code.tutsplus.com/tutorials/javascript-how-to-embed-private-members-into-an-object--cms-24287
var createProperty = function (obj, prop) {
var currentValue = obj[prop];
Object.defineProperty(obj, prop, {
get: function () { return currentValue; },
set: function (value) {
currentValue = value;
},
enumerable: true,
configurable: true
});
}
var entity = {
property: "hello world"
};
createProperty(entity, "property");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment