Reflect.get receiver Preserves Proxy Getters
Forward the third receiver argument in Reflect.get inside Proxy traps so inherited getters resolve this through the proxy rather than the raw target.
Code
const baseConfig = {
env: 'dev',
service: 'billing',
get endpointLabel() {
// Depends on `this.env`, so the `this` binding during lookup matters
return `${this.env}:${this.service}`;
},
};
const envOverride = Object.create(baseConfig);
envOverride.env = 'staging';
// Buggy proxy omits `receiver`, so `target[prop]` binds `this` to `target` ('staging')
const brokenProxy = new Proxy(envOverride, {
get(target, prop) {
if (prop === 'env') return 'prod-override';
return target[prop];
},
});
// Correct proxy forwards `receiver`, so inherited getters see the proxy's `env`
const fixedProxy = new Proxy(envOverride, {
get(target, prop, receiver) {
if (prop === 'env') return 'prod-override';
return Reflect.get(target, prop, receiver);
},
});
console.log('broken env :', brokenProxy.env);
console.log('broken label :', brokenProxy.endpointLabel);
console.log('fixed env :', fixedProxy.env);
console.log('fixed label :', fixedProxy.endpointLabel);Output
Captured from a sandboxed run of this snippet in the build pipeline.
broken env : prod-override
broken label : staging:billing
fixed env : prod-override
fixed label : prod-override:billingCode flow overview — Reflect.get receiver Preserves Proxy Getters
Top-level bindings baseConfig, envOverride, brokenProxy, fixedProxy hold the demo state — nothing sits inside a function scope.
Watch out for
- All referenced identifiers stay native to the language runtime — zero remote packages fetched.
- The guard
prop === 'env'bails cleanly ahead of remaining work via an earlyreturnwhen its condition holds.
Using it
The run prints broken env :, broken label :, fixed env : — each prefix names the step that produced it.
Key statements
if (prop === 'env') return 'prod-override';
return Reflect.get(target, prop, receiver);
return `${this.env}:${this.service}`;
Interactive Tools for This Topic
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
FAQ
- Will the snippet run without installing packages?
- Yes, it runs as-is — nothing outside the built-in APIs is referenced. Its state lives in module-level bindings: `baseConfig`, `envOverride`, `brokenProxy`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.