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