There is a class Account
and its instance acc1
, and I learned that ‘#’ can be used to get private field, so I think the #movements
property cannot be visited outside ‘acc1’, but I found that when I add a line of code console.log();
,it did return an error: Uncaught SyntaxError: Private field '#movements' must be declared in an enclosing class;
, but when I input acc1
in the console, I did see the exact value of that property, and when the code console.log(acc1);
also show the acc1 instance that contains #movements, but would’t the private property be hidden when log acc1, and wouldn’t it return an error when I input acc1.#movements?
The whole code is following:
class Account {
// 1) Public fields (instances)
locale = navigator.language;
// 2) Private fields (instances)
#movements = [];
#pin;
constructor(owner, currency, pin) {
this.owner = owner;
this.currency = currency;
this.#pin = pin;
}
}
const acc1 = new Account('Jay', 'EUR', 1);
console.log(acc1);
// console.log(acc1.#movements);
I have searched this question in stackoverflow but did not get the answer, maybe it’s because that’s too fundamental?