2

in normal object we can push to normal array value like obj.l =[]; obj.l.push("test")

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
6
  • 2
    prxy.h is not an Array, push is a method of Array. Commented Sep 26, 2017 at 13:12
  • 1
    push is a function of Array , but Proxy is an object Commented Sep 26, 2017 at 13:13
  • 1
    push() is for arrays, not objects, so use the right data structure. stackoverflow.com/questions/8925820/… Commented Sep 26, 2017 at 13:13
  • what do you want returned? Commented Sep 26, 2017 at 13:13
  • @Durga — Arrays are objects too. The significance is that Proxy is not an array. Commented Sep 26, 2017 at 13:13

1 Answer 1

5

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:

prxy.h.someKey = someValue;

Or using a dynamic key:

var dynamicKey = "car";
prxy.h[dynamicKey] = someValue;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.