Created
October 19, 2012 13:29
-
-
Save DavidBruant/3918227 to your computer and use it in GitHub Desktop.
ProxyMap. Maybe what objects should have been?
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
"use strict"; | |
(function(global){ | |
var handler = new Proxy({ | |
get: function(target, name){ | |
return target.get(name); | |
}, | |
set: function(target, name, value){ | |
target.set(name, value); | |
return true; | |
}, | |
delete: function(target, name){ | |
return target.delete(name) | |
}, | |
has: function(target, name){ | |
return target.has(name) | |
} | |
}, | |
/*throwingMetaHandler*/ { | |
get: function(target, trap){ | |
var op = target[trap]; | |
if(typeof op !== 'function') | |
throw new Error('The trap '+trap+' cannot be used') | |
else | |
return target[trap]; | |
} | |
}); | |
global.ProxyMap = function ProxyMap(){ | |
return new Proxy(new Map(), handler); | |
} | |
})(this); |
Suggestions:
-
There seems to be a closing parenthesis missing (line 29, instead of the curly brace.
-
Show a few examples of ProxyMap in use.
-
I’d set it up like this:
var ProxyMap = function () { ... return function () { ... }; }();
"use strict"; outside of IIFE? :(
Thanks for your comments. Weird at didn't get the notifications at the time...
@rwldrn what's the problem with the "use strict" outside the IIFE?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The meta-handler trick is here because I'm lazy to make all unwritten traps throw :-)