Good morning everyone,
I'm an enthusiastic ES6 amateur and I've recently approached the "Proxy object" subject for the first time. From the online material I've came across so far my understanding is that a Proxy is acting as a wrapper, on a generic object, that intercept actions on it in order to change its behavior. With this in mind I've tried to run the following snippet and the outcome is surprising (at least for me):
"use strict";
let user = {
id: {
name: "Johnny",
surname: "Potts",
},
age: 44,
};
let userProxy = new Proxy(user, {
get(target, key, receiver) {
alert("GET triggered");
return Reflect.get(...arguments);
},
set(target, key, value, receiver) {
alert("SET triggered");
return Reflect.set(...arguments);
},
});
userProxy.id.name = "Pete"; //change an existing property in the nested object.
userProxy.id.gender = "male"; //introduce a new property in the nested object.
As you'll easily notice by running this code, the "GET" trap triggers every time instead of "SET" and the new property doesn't get written inside the "user" object however the existing one gets updated.
Can anyone explain me why? Thanks in advance for your time and support.