Closed
Description
- Version: v10.5.0
- Platform: Darwin RWK-Mac-2017.local 17.3.0 Darwin Kernel Version 17.3.0: Thu Nov 9 18:09:22 PST 2017; root:xnu-4570.31.3~1/RELEASE_X86_64 x86_64
- Subsystem: util
'Ran into this in the process of making virtual properties on a Proxy object enumerable.
See tc39/ecma262#367 (comment)
Briefly, customizing the enumeration of a Proxy object requires implementing both an ownKeys()
method (to define the keys to be enumerated) and getOwnPropertyDescriptor()
to declare those keys as enumerable. The problem here is that property values in Proxy objects are not canonically defined by a property descriptor the way they are with in a vanilla JS object; they're defined by the Proxy handler.get()
method. This leads to util.inspect
showing seemingly undefined values that, when referenced directly, are actually defined ...
> x = new Proxy({a: 1, b:2}, {
... get(o, k) {return k == `c` ? o.a + o.b : o[k];},
... ownKeys(o) {return [...Object.keys(o), 'c']},
... getOwnPropertyDescriptor(o, k) {return {configurable: true, enumerable: true}},
... })
Proxy [ { a: 1, b: 2 },
{ getOwnPropertyDescriptor: [Function: getOwnPropertyDescriptor],
ownKeys: [Function: ownKeys],
get: [Function: get] } ]
> console.log(x)
{ a: undefined, b: undefined, c: undefined }
undefined
> x.a
1
> x.b
2
> x.c
3
>