Created
November 25, 2011 10:27
-
-
Save Maxdamantus/1393215 to your computer and use it in GitHub Desktop.
Non-leaky implementation of Crockford's make_sealer
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
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