Skip to content

Instantly share code, notes, and snippets.

@Maxdamantus
Created November 25, 2011 10:27
Show Gist options
  • Save Maxdamantus/1393215 to your computer and use it in GitHub Desktop.
Save Maxdamantus/1393215 to your computer and use it in GitHub Desktop.
Non-leaky implementation of Crockford's make_sealer
function make_sealer(){
var cur = null, ret;
return {
sealer: function write(value){
return function f(){
if(f === cur)
ret = value;
};
},
unsealer: function read(box){
ret = undefined;
cur = box;
box();
cur = null;
return ret;
}
};
}
var s = make_sealer();
var secret = s.sealer(42), magic = s.sealer("forty two");
print([secret, magic].map(s.unsealer));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment