Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ProxyMap. Maybe what objects should have been?
"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);
@DavidBruant

This comment has been minimized.

Copy link
Owner Author

@DavidBruant DavidBruant commented Oct 19, 2012

So basically, it's a wrapper around maps to make them look like objects. It's just syntactic sugar.
The interesting part is that it's an object with all the ES5 property descriptor details removed.

@DavidBruant

This comment has been minimized.

Copy link
Owner Author

@DavidBruant DavidBruant commented Oct 19, 2012

The meta-handler trick is here because I'm lazy to make all unwritten traps throw :-)

@rauschma

This comment has been minimized.

Copy link

@rauschma rauschma commented Oct 20, 2012

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 () { ... };
    }();
    
@rwaldron

This comment has been minimized.

Copy link

@rwaldron rwaldron commented Oct 20, 2012

"use strict"; outside of IIFE? :(

@DavidBruant

This comment has been minimized.

Copy link
Owner Author

@DavidBruant DavidBruant commented Nov 26, 2012

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