Created
January 10, 2013 19:56
-
-
Save rwaldron/4505255 to your computer and use it in GitHub Desktop.
Example of WeakMap holding private data for module-defined and exported class
This file contains 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
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