Vue/Vuex: Proxy对象中区分数组和对象

Vue/Vuex: Distinguish arrays from objects in Proxy objects

here所述,反应式 Vuex 对象作为代理对象返回。在大多数情况下这可能不是问题,但是如何确定代理是来自数组还是对象?

Proxy 是 array/object 之上的透明层,因此您无需确定 Proxy 的原始来源。

变量本身应该被视为 Proxy 层不存在。如果它是 ArrayProxy,则将变量视为 Array,对于 Object 也是如此。 运行 以下代码段为示例。

const arr = [1,2,3]
const arrProxy = new Proxy(arr, {})             // value is identical to `arr`
console.log(arrProxy.map(x => x * 10))          // => [ 10, 20, 30 ]
console.log('isArray', Array.isArray(arrProxy)) // => true

const obj = { foo: true, bar: false }
const objProxy = new Proxy(obj, {})            // value is identical to `obj`
console.log(Object.keys(objProxy))             // => [ 'foo', 'bar' ]
console.log('objArray type:', typeof objProxy) // => object