const data = { user: { name: '', age: ''} }
data.user.name = 'Boris'
data[user.name] = 'Boris'
const obj = {
a: {
b: {
c: {
xxx: 666,
},
},
},
};
const proxy = new Proxy(obj, {
get(target, key) {
return key.split('.').reduce((p, c) => p?.[c], target);
},
set(target, key, val) {
const keys = key.split('.');
const k = keys.pop();
keys.reduce((p, c) => p[c] ??= {}, target)[k] = val;
return true;
},
});
console.log(proxy['a.b.c.xxx']); // 666
proxy['x.y.z'] = 187;
console.log(obj.x.y.z); // 187