5

I am working with ES6 Proxy. I have created a proxy of an array, now when i check the type of proxy it is giving me as Object type.

Question:

How can i check if the proxy i have created was for array or object?

Example:

const arr = ['a', 'b', 'c'];

const arrProxy = new Proxy(arr, {});

alert(typeof(arrProxy));

UPDATE (SOLUTION): Instead of using typeof, we should use Array.isArray

const arr = ['a', 'b', 'c'];

const arrProxy = new Proxy(arr, {});

alert(Array.isArray(arrProxy));
Johar Zaman
  • 1,955
  • 17
  • 27

3 Answers3

5

You can't tell that a proxy is a proxy. That's part of the point of them, they provide a facade (one you can't detect) around another object.

As far as code looking at your arrProxy can tell, it's an array:

const arr = ['a', 'b', 'c'];

const arrProxy = new Proxy(arr, {});

console.log(Array.isArray(arrProxy)); // true

Separately: typeof is very general, it gives you "object" for a huge range of things: Anything that's of an object (not primitive) type (including null). So typeof new Map(), typeof new Set(), typeof null, typeof document (on browsers), etc., will all give you "object". (Also note that typeof is an operator, not a function; no need for the () in your code sample.)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
2

As other answers suggest, you cannot tell if something is a proxy.

So you might need to implement it yourself.

Here is an example from: https://exploringjs.com/deep-js/ch_proxies.html#transparent-virtualization-and-handler-encapsulation

const proxies = new WeakSet();

export function createProxy(obj) {
  const handler = {};
  const proxy = new Proxy(obj, handler);
  proxies.add(proxy);
  return proxy;
}

export function isProxy(obj) {
  return proxies.has(obj);
}
Lydia Yuan
  • 21
  • 2
1

There is also one way to do this using instanceof:

if (arrProxy instanceof Array) {
   console.log('This is an array!');
}
ABMagil
  • 4,579
  • 4
  • 21
  • 35
marsibarsi
  • 973
  • 7
  • 7
  • 1
    That will fail for any array you receive from another *realm*. Which is why we have `Array.isArray`. – T.J. Crowder Apr 02 '19 at 16:58
  • 1
    What do you mean by `another realm` here? – Johar Zaman Apr 02 '19 at 16:59
  • 3
    @JoharZaman - A [realm](https://tc39.github.io/ecma262/#sec-code-realms) *"...consists of a set of intrinsic objects, an ECMAScript global environment, all of the ECMAScript code that is loaded within the scope of that global environment, and other associated state and resources."* So for instance, if you have a page with an iframe in it, the page and the iframe are separate realms; if you pass an array from one to the other, `instanceof` won't work. Similarly if one window opens another, if you pass data between them, that data is crossing realms. – T.J. Crowder Apr 02 '19 at 17:02
  • @marsibarsi There is a problem with `instanceof`. When i am using `arrProxy instanceof Object` it still gives me true. – Johar Zaman Apr 02 '19 at 17:03
  • `__proto__` of `Array` is `Object` that is why `arrProxy instanceof Object` is also `true` – marsibarsi Apr 02 '19 at 17:05