Andreas Gal discovered that in order to wrap the DOM faithfully, proxies need to trap
instanceof. Apparently, the DOM implements some kind of multiple inheritance relationship that can only be emulated by trapping
instanceof. (the issue has to do with “mixin” interfaces such as
A proposed extension to the Proxy API is to enable proxies to trap
instanceof when used as the right-hand operand. That is, if
p is a proxy with handler
x instanceof p traps as
hasInstance trap would be a derived (optional) trap. If absent,
x instanceof p defaults as follows:
pis an object proxy, throw a
TypeError(this mimics step 6 of algorithm 11.8.6)
pis a function proxy, default to the built-in
[[HasInstance]]behavior for Functions.
hasInstance trap can be triggered by providing proxies with a custom implementation of the
[[HasInstance]] built-in method:
When the [[HasInstance]] internal method of a proxy
P is called with argument
V, the following steps are taken:
Pis an object proxy, throw a TypeError (as per ES5 section 11.8.6, step 6).
Pis a function proxy, return the result of performing the default [[HasInstance]] behavior for Functions (as per ES5 section 220.127.116.11).
There is no need to modify the evaluation of
instanceof in ES5 section 11.8.6 as it just calls the right-hand operand’s
[[HasInstance]] internal method if present, which redirects to the above implementation if the right-hand operand is a proxy.
TC39 November 2010 meeting: after a meeting with the Web IDL editors, agreed that Web IDL should not expose the multiple inheritance. Hence, a custom [[HasInstance]] would not be necessary to wrap these host objects.
There may be other use cases for a custom [[HasInstance]] (outside of Web IDL), but until such cases are identified we agreed not to pursue this strawman in the interest of not further complicating the Proxy API.
— Tom Van Cutsem 2010/11/19 3:00