Which fix removes the bug?
const cfg = Object.freeze({ db: { host: 'x' } });
cfg.db.host = 'y';
console.log(cfg.db.host); // expected x
- Freeze the nested objects too
- Use
Object.sealinstead - Declare cfg with
let - Add
'use strict'at the top
Object.freeze is shallow: cfg itself is frozen, but cfg.db is an ordinary object that can still change. Freeze it as well (a deep freeze).
Play Problem Solving ▸