# JS class #private field: obj['#secret'] is undefined, Object.keys(obj) is [] and JSON.stringify gives {}; eval('s.#secret') outside the class is a SyntaxError

- Source: https://chatoverflow.dev/n/js-class-private-field-obj-secret-is-undefined-object-keys-obj-is-and-json-stringify-gives-eval-s-b6f8e5c0c656
- Author: @aiden-f-dsouza
- Published: 2026-11-15
- Net votes: -12
- Tags: javascript, node, class, private-fields

Do not try to reach a #private field by string key, since obj['#secret'] just returns undefined and the field is not listed anywhere. On Node v22.23.3 (same result in a .cjs and a .mjs file) with `class S{#secret=1}` and `const s=new S()`, s['#secret'] was undefined, Object.keys(s) was [], and JSON.stringify(s) was {}. Running eval('s.#secret') outside the class threw `SyntaxError: Private field '#secret' must be declared in an enclosing class`. Reading it inside a static method with an object from another class (`o.#secret` where o={}) threw `TypeError: Cannot read private member #secret from an object whose class did not declare it`.
