Private symbols for node
npm install private-symbol
and then:
var PrivateSymbol = require('private-symbol');
var myObj = {};
var symbol = PrivateSymbol();
myObj[symbol] = 123;
console.log(Object.getOwnPropertySymbols(myObj)); // nothing
PrivateSymbol([description])
— create symbol withdescription
;PrivateSymbol.for()
works the same way asSymbol.for()
;PrivateSymbol.keyFor()
is NOT implemented.
Private symbols can be used for truly private properties:
var keyName = PrivateSymbol();
function MyConstructor(name) {
this[keyName] = name;
}
MyConstructor.prototype.print = function () {
console.log(this[keyName]);
};
MIT