2017-09-26 65 views
0

在正常的對象,我們可以推到正常的數組值如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://stackoverflow.com/questions/8925820/javascript-object-push-function –

回答

4

您不能在對象上使用數組方法。無論如何,這裏真的不會有任何意義。沒有理由使用push()時,你可以一定的價值,附加到一個對象:

prxy.h.someKey = someValue; 
相關問題