Joh*_*man 5 javascript es6-proxy
我正在使用 ES6 代理。我已经创建了一个数组的代理,现在当我检查代理的类型时,它给我作为Object类型。
问题:
如何检查我创建的代理是用于数组还是对象?
例子:
const arr = ['a', 'b', 'c'];
const arrProxy = new Proxy(arr, {});
alert(typeof(arrProxy));Run Code Online (Sandbox Code Playgroud)
更新(解决方案):typeof我们应该使用
而不是使用Array.isArray
const arr = ['a', 'b', 'c'];
const arrProxy = new Proxy(arr, {});
alert(Array.isArray(arrProxy));
Run Code Online (Sandbox Code Playgroud)
你无法判断代理就是代理。这是它们的一部分要点,它们在另一个对象周围提供了一个外观(您无法检测到)。
据查看您的代码arrProxy可以看出,它是一个数组:
const arr = ['a', 'b', 'c'];
const arrProxy = new Proxy(arr, {});
console.log(Array.isArray(arrProxy)); // trueRun Code Online (Sandbox Code Playgroud)
另外:typeof非常通用,它为您提供了各种各样的东西:任何对象(非原始)类型的东西(包括)。所以,,,,(在浏览器上)等等,都会给你。(另请注意,这是一个运算符,而不是函数;代码示例中不需要。)"object"nulltypeof new Map()typeof new Set()typeof nulltypeof document"object"typeof()