Last active
August 29, 2015 14:24
-
-
Save simaovergalho/9775a0c8fd135da4e746 to your computer and use it in GitHub Desktop.
Embed Private Members Into an Object
This file contains hidden or 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
//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