Last active
September 10, 2018 07:59
-
-
Save juandopazo/2901426 to your computer and use it in GitHub Desktop.
Privates and WeakMaps
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
// Based on a gist by @rwaldron | |
// https://gist.github.com/2897761 | |
function privatize() { | |
var map = new WeakMap(); | |
return function private(obj) { | |
var data = map.get(obj); | |
if (!data) { | |
map.set(obj, data = {}); | |
} | |
return data; | |
}; | |
} | |
var Person = (function () { | |
var private = privatize(); | |
function Person(name) { | |
private(this).name = name; | |
} | |
Person.prototype.say = function (msg) { | |
return private(this).name + ' says: ' + msg; | |
}; | |
return Person; | |
}()); |
Why not just:
var Person = (function () {
var private = {};
function Person(name) {
private.name = name;
}
Person.prototype.say = function (msg) {
return private.name + ' says: ' + msg;
};
return Person;
}());
Because that would share the private data with all instances of Person. It would be the same as:
var Person = (function () {
var _name;
function Person(name) {
_name = name;
}
Person.prototype.say = function (msg) {
return _name + ' says: ' + msg;
};
return Person;
}());
var bob = new Person('Bob');
var peter = new Person('Peter');
bob.say('Hi!'); // Peter says: Hi!
That means it needs to be a map. And it needs to be a WeakMap so that the private record is GC'ed when the class instance is GC'ed.
why not:
const _ = new WeakMap()
const private = (instance) => {
return _.get(instance)
}
class Person {
constructor (name) {
private(this).name = name
}
say (msg) {
return private(this).name + 'says: ' + msg
}
}
?
EDIT:
Actually, private
should be defined as const private = (instance) => _.has(instance) ? _.get(instance) : _.set(instance)
hey @juandopazo, i didn't get this
Because that would leak the data to anyone with a reference to the instance.
can you elaborate more
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
As a side note, this is "class private". Which means you can access private data of other members of the same class. For example: