Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Created December 12, 2011 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBruant/1466999 to your computer and use it in GitHub Desktop.
Save DavidBruant/1466999 to your computer and use it in GitHub Desktop.
proxy as argument would help here
function makeProxyWhichCallOnDelete(farg){
var target = {}; // Important: the constructor was not initialized with the target
return new Proxy(target, {
delete: function(target, name){
farg(target); // no access to the proxy identity!
}
});
}
(function(){
var values = new WeakMap();
function f(o){
console.log(values.get(o));
}
var p = makeProxyWhichCallOnDelete(f);
p2 = makeProxyWhichCallOnDelete(f);
values.set(p, 1); // Setting the value with the proxy, not the target!
values.set(p2, 2);
p.prop = "yo";
delete p.prop; // trap called
// f will receive the target as argument while it has values stored
// with proxy identities
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment