Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 457
Tahsis Claus Avatar asked Feb 27 '17 17:02

Tahsis Claus


People also ask

What is the use of proxy object in JavaScript?

Proxy objects are commonly used to log property accesses, validate, format, or sanitize inputs, and so on. You create a Proxy with two parameters: target : the original object which you want to proxy. handler : an object that defines which operations will be intercepted and how to redefine intercepted operations.

What does proxy mean in JavaScript?

Proxy is an object in javascript which wraps an object or a function and monitors it via something called target. Irrespective of the wrapped object or function existence. Proxy are similar to meta programming in other languages.

What is ES6 proxy?

ES6 implements intercession form of meta programming using Proxies. Similar to ReflectAPI, the Proxy API is another way of implementing meta programming in ES6. The Proxy object is used to define custom behavior for fundamental operations. A proxy object performs some operations on behalf of the real object.

What is proxy value?

Proxy Measures A proxy is an indirect measure of the desired outcome which is itself strongly correlated to that outcome. It is commonly used when direct measures of the outcome are unobservable and/or unavailable.


1 Answers

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. So if you pass it a function (they all implement [[Call]]), then the proxy will too, and the proxy will return function for typeof.

like image 99
loganfsmyth Avatar answered Oct 21 '22 14:10

loganfsmyth