JSFiddle - React, Tailwind, and code Playground
JavaScript
var privates = (function(){
var key = 1;
function next_private_key() {
key += 1;
return key;
}
function privates(owner, obj) {
if (typeof owner.__privates === 'undefined') {
owner.__privates = {};
}
if (typeof obj.__private_key === 'undefined') {
obj.__private_key = next_private_key();
}
if (typeof owner.__privates[obj.__private_key] === 'undefined') {
owner.__privates[obj.__private_key] = {};
}
return owner.__privates[obj.__private_key];
}
return privates;
})();
var a = {}, b = {}, c = {};
privates(a, b).foo = 10;
privates(c, a).bar = 20;
console.log(privates(a, b).foo); // 10
console.log(privates(a, c).foo); // undefined
console.log(privates(c, b).foo); // undefined
console.log(privates(c, a).bar); // 20