Mrs. Yanti’s savings house: #saldo sits in the back room only the class’s own code can enter. What happens when Dina’s outside script names acc.#saldo, and…
Real encapsulation: other code (or you, later) cannot tamper with internal state, and that state does not leak into Object.keys or JSON. Unlike _underscore (only a convention) and closures (private, but without a class). The foundation of safe modern classes.
# is not a convention: it is privacy the language enforces
acc['#saldo'] gives undefined, not the saldo That is an ordinary property named by a string, not the private field. A private field cannot be reached with brackets, only as this.#saldo inside the class.acc.#saldo straight from outside stops the code from running at all Not undefined: a SyntaxError while the script is read, before any line runs. Provide a getter or a public method if it needs reading. (The DevTools console deliberately lets you peek at a #field for debugging; in real code it stays a SyntaxError.)JSON.stringify A #field never goes into JSON.stringify/Object.keys. To serialise it, expose it through toJSON() or a getter.acc.setor(50) A method inside the class may change #saldo; Rizal goes into the back room.acc['#saldo'] undefined: that string names an ordinary property that does not exist.acc.#saldo from outside A SyntaxError while read: even the setor line above it does not run.JSON.stringify(acc) #saldo is left behind; toJSON() can take it along on purpose.