Created
December 12, 2011 12:49
-
-
Save DavidBruant/1466999 to your computer and use it in GitHub Desktop.
proxy as argument would help here
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 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