Skip to content

Instantly share code, notes, and snippets.

@maurobringolf
Last active December 22, 2016 16:09
Show Gist options
  • Save maurobringolf/a0a2914639a4a901238b5e4f6084a70f to your computer and use it in GitHub Desktop.
Save maurobringolf/a0a2914639a4a901238b5e4f6084a70f to your computer and use it in GitHub Desktop.
//defining a pseudo-private property o.x using getters and setters
var o = {
__c: 1,
get x() { return this.__c; },
set x(y) { this.__c = y; }
}
//Make the property o.__c non-enumerable
Object.defineProperty(o, '__c', { enumerable: false });
//Object.keys and for...in loop skip x.c now
Object.keys(o); //["x"]
//The property can still be accessed using its name
o.__c = 2;
o.__c; //2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment