I was actually going to answer my question myself, but I was too slow, and others answered it first (and added very useful information besides). Nevertheless, here is the answer I wanted to write, in case it might help someone somehow:
It appears that the getOwnPropertyDescriptor
trap is fired when hasOwnProperty
is called. So you can trap hasOwnProperty
by doing the following:
getOwnPropertyDescriptor(target, name) {
return {
value : target[name],
//use a logical set of descriptors:
enumerable : true,
configurable : true,
writable : true
};
}
The other part is trapping get
and ownKeys
as well:
get(target, name) {
return {}; //replace this with a relevant object
}
//These will be iterated over as part of the for loop
ownKeys() {
return ["RelevantProperty1", "RelevantProperty2"];
}
All in all, since you have to return an array of properties when you trap ownKeys
, using a proxy doesn't seem to make things much better in this use case. I think for the majority of situations, the following would work just as well and be less fragile:
let obj = {};
let keys = ["a" , "b"];
for (let key of keys) {
obj[key] = {}; //relevant object
}
So using a proxy might be overkill.