简体   繁体   English

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

[英]How to push to an array value in proxy object in Javascript

in normal object we can push to normal array value like obj.l =[];在普通对象中,我们可以推送到普通数组值,如 obj.l =[]; obj.l.push("test") obj.l.push("测试")

Example.例子。

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

You can't use array methods on an object.不能在对象上使用数组方法。 And there really wouldn't be a point here anyway.无论如何,这里真的没有任何意义。 There's no reason to use push() when you can just append a value to an object:当您可以将值附加到对象时,没有理由使用push()

prxy.h.someKey = someValue;

Or using a dynamic key:或者使用动态密钥:

var dynamicKey = "car";
prxy.h[dynamicKey] = someValue;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM