如何推送到代理对象中的数组值在使用Javascript

问题描述:

在正常的对象,我们可以推到正常的数组值如obj.l = []; obj.l.push(“测试”)如何推送到代理对象中的数组值在使用Javascript

示例。

var prxy = new Proxy({} , { 
get(target, name){ 
    return target[name] 
}, 
set(target,name, value){ 
    target[name] = value; 
    return true; 
} 
}) 

prxy.h = {test : "test"} 
>> {test: "test"} 
prxy.h 
>>{test: "test"} 
prxy.h.push("test") 
>>VM2724:1 Uncaught TypeError: prxy.h.push is not a function 
at <anonymous>:1:8 
+2

'prxy.h'不是一个数组,'push'是阵列的方法。 – gurvinder372

+1

push是Array的一个函数,但Proxy是一个对象 – Durga

+1

push()是用于数组,而不是对象,所以使用正确的数据结构。 https://*.com/questions/8925820/javascript-object-push-function –

您不能在对象上使用数组方法。无论如何,这里真的不会有任何意义。没有理由使用push()时,你可以一定的价值,附加到一个对象:

prxy.h.someKey = someValue;