【发布时间】:2022-01-05 20:11:56
【问题描述】:
如here 所述,反应式 Vuex 对象作为代理对象返回。在大多数情况下这可能不是问题,但我如何确定 Proxy 是来自数组还是对象?
【问题讨论】:
-
Array.isArray仍应为数组返回 true。
标签: javascript vue.js vuex
如here 所述,反应式 Vuex 对象作为代理对象返回。在大多数情况下这可能不是问题,但我如何确定 Proxy 是来自数组还是对象?
【问题讨论】:
Array.isArray 仍应为数组返回 true。
标签: javascript vue.js vuex
Proxy 是数组/对象顶部的透明层,因此您无需确定Proxy 的原始来源。
变量本身应该被视为Proxy 层不存在。如果它是Array 的Proxy,则将变量视为Array,Object 也是如此。运行以下代码 sn -p 示例。
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
【讨论】: