Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created January 10, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwaldron/4505255 to your computer and use it in GitHub Desktop.
Save rwaldron/4505255 to your computer and use it in GitHub Desktop.
Example of WeakMap holding private data for module-defined and exported class
module TimeCard {
let wm = new WeakMap();
export class TimeCard {
constructor( time ) {
wm.set(this, { time });
}
time( value = undefined ) {
if ( value === undefined ) {
return wm.get(this).time;
}
wm.get(this).time = value;
return this;
}
}
}
import { TimeCard } from TimeCard;
var card = new TimeCard(Date.now());
console.log(card.time());
setTimeout(function() {
console.log(
card.time(Date.now()).time()
);
}, 10);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment