Tahsis Claus Tahsis Claus -4 years ago 94
Javascript Question

Is it possible to intercept the "typeof" operation with an ES2016 Proxy?

Is it possible to define a handler property that intercepts

typeof proxyObject
? None of the listed traps at Mozilla list it as an interception.

Answer Source

It is not possible to intercept typeof. The type of the proxy object will be the same as the proxy target.

You can see this in the spec here: http://www.ecma-international.org/ecma-262/7.0/#sec-typeof-operator-runtime-semantics-evaluation with the important parts of the table being

  1. Object (ordinary and does not implement [[Call]]) => "object"
  2. Object (implements [[Call]]) => "function"

A proxy is an object, and whether it implements [[Call]] depends on the type of target passed to the proxy at creation time. That you can see in http://www.ecma-international.org/ecma-262/7.0/#sec-proxycreate Step 7.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download