Javascript's new Proxy feature offers some interesting features for debugging. For example you can "defend" an object by putting it behind a Proxy with a get
handler that throws if you access an undefined property. This helps catch typos and other kinds of mistakes.
This could be used something like this:
class Something {
constructor()
{
this.foo = "bar";
allObjects.push(this);
}
};
function defend(o)
{
return new Proxy(o, DebugCheckHandler);
};
let rawSomething = new Something();
let defendedSomething = defend(rawSomething);
Code can be diligently written to only deal in terms of defendedSomething
. However in this example, the Something
constructor passes this
somewhere else (to allObjects
). This ends up having the same effect as using both rawSomething
and defendedSomething
across the codebase.
Problems then arise from the fact the proxy reference does not equal its original reference, since rawSomething !== defendedSomething
. For example, allObjects.includes(defendedSomething)
will return false if it contains rawSomething
, because includes
does a strict ===
check.
Is there a good way to solve this without making too many changes to the code?
allObjects.push(this);
should be outside, e.g.allObjects.push(rawSomething);
– Eba