javascript - How to check the type of es6 proxy in Javascript?
问题描述
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.)
推荐阅读
- excel - 仅在更改初始单元格值后添加注释的 VBA 代码?
- xml - XSL 到 HTML:包含方法的问题
- azure - Azure AD B2C 中用于登录/注册的自定义 UI 给出错误
- reporting-services - SSRS 报告自动生成
- c++ - 为什么我的银行管理系统无法正常工作?
- audiokit - Audiokit 4.9 中的 AKSequencer.init()
- excel - 模拟简单的 VBA 宏以与 Google 表格兼容
- github - 列出特定存储库的所有分支
- python - xgb-wrapper 交叉验证 (xgb.cv) 的结果是什么?
- ios - Tableview 没有得到 Xib 文件的高度