Skip to main content
added 299 characters in body
Source Link
Jared Smith
  • 22.3k
  • 5
  • 50
  • 98

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => { let privateData = new WeakMap(); return class { constructor () { privateData.set(this, { hidden: 'bar' }); } // this is a prototype function, we could equivalently define // it outside the class definition as Foo.prototype.getPrivate // and it would be the same getPrivate () { return privateData.get(this).hidden; } } })(); console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is returned, code outside the IIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo { constructor () { super(); // all good thanks to super } } // using non ES-6 function Foo () { privateData.set(this, {...}); } Foo.prototype.getPrivate = function() { return privateData.get(this); }); function Bar() { Foo.call(this); // fine here } Bar.prototype = Foo.prototype; 

The only limitation is that anything you want to be able to access the private data has to be defined inside the IIFE, you can't come along later and add a method to Foo's prototype. Although, if you're that concerened with performance, you shouldn't be altering prototypes willy-nilly anyway.

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => { let privateData = new WeakMap(); return class { constructor () { privateData.set(this, { hidden: 'bar' }); } // this is a prototype function, we could equivalently define // it outside the class definition as Foo.prototype.getPrivate // and it would be the same getPrivate () { return privateData.get(this).hidden; } } })(); console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is returned, code outside the IIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo { constructor () { super(); // all good thanks to super } } // using non ES-6 function Foo () { privateData.set(this, {...}); } Foo.prototype.getPrivate = function() { return privateData.get(this); }); function Bar() { Foo.call(this); // fine here } Bar.prototype = Foo.prototype; 

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => { let privateData = new WeakMap(); return class { constructor () { privateData.set(this, { hidden: 'bar' }); } // this is a prototype function, we could equivalently define // it outside the class definition as Foo.prototype.getPrivate // and it would be the same getPrivate () { return privateData.get(this).hidden; } } })(); console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is returned, code outside the IIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo { constructor () { super(); // all good thanks to super } } // using non ES-6 function Foo () { privateData.set(this, {...}); } Foo.prototype.getPrivate = function() { return privateData.get(this); }); function Bar() { Foo.call(this); // fine here } Bar.prototype = Foo.prototype; 

The only limitation is that anything you want to be able to access the private data has to be defined inside the IIFE, you can't come along later and add a method to Foo's prototype. Although, if you're that concerened with performance, you shouldn't be altering prototypes willy-nilly anyway.

added 496 characters in body
Source Link
Jared Smith
  • 22.3k
  • 5
  • 50
  • 98

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => { let privateData = new WeakMap(); class Foo return class {   constructor () {   privateData.set(this, {   hidden: 'bar'   });   }   // this is a prototype function, we could equivalently define   // it outside the class definition as Foo.prototype.getPrivate   // and it would be the same   getPrivate () {   return privateData.get(this).hidden;   }  } })(); console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is exportedreturned, code outside the moduleIIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo { constructor () { super(); // all good thanks to super } } // using non ES-6 function Foo () { privateData.set(this, {...}); } Foo.prototype.getPrivate = function() { return privateData.get(this); }); function Bar() { Foo.call(this); // fine here } Bar.prototype = Foo.prototype; 

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

let privateData = new WeakMap(); class Foo { constructor () { privateData.set(this, { hidden: 'bar' }); } // this is a prototype function, we could equivalently define // it outside the class definition as Foo.prototype.getPrivate // and it would be the same getPrivate () { return privateData.get(this).hidden; } } console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is exported, code outside the module its defined in can't access privateData. See this for more info.

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

const Foo = (() => { let privateData = new WeakMap();  return class {   constructor () {   privateData.set(this, {   hidden: 'bar'   });   }   // this is a prototype function, we could equivalently define   // it outside the class definition as Foo.prototype.getPrivate   // and it would be the same   getPrivate () {   return privateData.get(this).hidden;   }  } })(); console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is returned, code outside the IIFE its defined in can't access privateData. See this for more info.

Note that this won't necessarily break inheritance depending on how you're doing it:

class Baz extends Foo { constructor () { super(); // all good thanks to super } } // using non ES-6 function Foo () { privateData.set(this, {...}); } Foo.prototype.getPrivate = function() { return privateData.get(this); }); function Bar() { Foo.call(this); // fine here } Bar.prototype = Foo.prototype; 
Source Link
Jared Smith
  • 22.3k
  • 5
  • 50
  • 98

This can be accomplished in ES 6, but I still think its largely unnecessary except at the module-level:

let privateData = new WeakMap(); class Foo { constructor () { privateData.set(this, { hidden: 'bar' }); } // this is a prototype function, we could equivalently define // it outside the class definition as Foo.prototype.getPrivate // and it would be the same getPrivate () { return privateData.get(this).hidden; } } console.log(new Foo().getPrivate()); // logs 'bar' 

Importance of the WeakMap:

It allows arbitrary objects as keys, but those keys are weakly held: they don't count as references to prevent the key object from being garbage collected. Once the class is exported, code outside the module its defined in can't access privateData. See this for more info.