6

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));
2
  • 1
    typeof [] is also 'object', try Array.isArray(arrProxy) Commented Apr 2, 2019 at 16:55
  • Are you asking how to tell that arrProxy is an array, or how to tell it's a proxy? Commented Apr 2, 2019 at 16:58

3 Answers 3

6

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.)

Sign up to request clarification or add additional context in comments.

Comments

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);
}

Comments

1

There is also one way to do this using instanceof:

if (arrProxy instanceof Array) {
   console.log('This is an array!');
}

5 Comments

That will fail for any array you receive from another realm. Which is why we have Array.isArray.
What do you mean by another realm here?
@JoharZaman - A realm "...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.
@marsibarsi There is a problem with instanceof. When i am using arrProxy instanceof Object it still gives me true.
__proto__ of Array is Object that is why arrProxy instanceof Object is also true

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.