I'm reading the documentation here, and it seems to imply that even for private fields, it would be possible to access them via a Proxy. See the blurb that starts with "....To fix this....". But the example given, doesn't work. My code is shown below:
The class with the private field:
class Danger {
#areYouSure = "magic";
grave(){
console.log("Yes, grave danger");
return true;
}
}
module.exports = Danger;
Trying to use a proxy to access it:
const Danger = require('./testing-private');
const handler = {
get(target, property, receiver){
return target[property];
}
};
const dangerProxy = new Proxy(new Danger(), handler);
console.log("--------------------------------------", dangerProxy.areYouSure);
This prints out:
-------------------------------------- undefined
If I replace "dangerProxy.areYouSure" with "dangerProxy.#areYouSure". I get
/home/zaphod/code/math/js-experiments/src/danger-proxy.js:29
console.log("--------------------------------------", dangerProxy.#areYouSure);
^
SyntaxError: Private field '#areYouSure' must be declared in an enclosing class
at Object.compileFunction (node:vm:352:18)
at wrapSafe (node:internal/modules/cjs/loader:1032:15)
at Module._compile (node:internal/modules/cjs/loader:1067:27)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47
Is there a way to get this working or are my expectations incorrect?