Private Declarations
Bradley Farias
Driving Questions
Private State
class Foo {� #bar = 0;� get bar() {� return this.#bar++;� }�};
Private State
const privy = new WeakMap();�const foo = {� get bar() {� if (!privy.has(this)) throw …� let _ = privy.get(this);� privy.set(this, _ + 1);� return _;� }�};
Private State
const privy = new WeakMap();�const foo = {� get bar() {� if (!privy.has(this)) throw …� let _ = privy.get(this);� privy.set(this, _ + 1);� return _;� }�};
privy.set(foo, 0);
Interceptable via some means of mutating state.
Private State
private #bar;�foo.#bar;�
Private State
private #bar;�const foo = {� get bar() {� return this.#bar++;� }�};�foo.#bar = 0;�console.log(foo.#bar);
#bar on value that does not have an initialized field #bar could lead to impersonation and/or accidental "Branding"
Private State
private #bar;�const foo = {� get bar() {� return this.#bar++;� }�};�private.initialize(foo.#bar, 0);
Similar concept to new.initialize, syntax/api bikeshed could go all over the place
Private State
const bar = 'outside';�class Foo {� [bar] = … � bar = … �};
Private State
private #baz;
const bar = 'outside';�class Foo {� [bar] = … � bar = … �� [#baz] = 0� #baz = … �};
Since `#baz = 0` has meaning inside classes, take computed property precedence of pulling from outside context
Private State
private #bar;�
const bar = 'ignored';�const foo = {� [#bar]: 0,� bar: function () {� return this.#bar++;� }�};�
#bar = …;�foo = {};�private.initialize(foo.#bar, 0);
Object.defineProperty(foo, 'bar', {value: …});
Private State
const bar = 'ignored';�const foo = {� bar: function () {� }�};�
Private State
private #bar;�const foo = {� [#bar]: 0,� … �};�const baz = {� [#bar]: 0,� … �};�
Rambling: SHARING IS CARING
Gateway pattern
// Use cycles of modules to form a timing �// the modules are both initialized but not evaluated�// remove a setup function on evaluation
import {setup as setupFriend} 'friend';�var SECRET = setupFriend();�export function setup() {� setup = null;� if (shared) return;� SECRET = …;� return SECRET;�}
Cycles, timing, and a whole lotta trust going on here to do this right
Getters/setters across modules :-(
private #snacks;�function snacks(o, v) {� return o.#snacks = v;�}�export {snacks};
Limit who can get to private state
private #snacks;�function snacks(o) {� … �}�export {snacks} for 'friend';
BIKESHEDS
private #snacks;�export {#snacks} for 'friend';�
import {#snacks as #yums} from 'other';