JavaScript recently added private class fields, named with a hash prefix.
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_class_fields)
I'm looking for a clear answer on whether this form of privacy is 'per class' or 'per instance.'
Say a Person class is defined with a private field named '#secret'.
I use 'new' to construct two instances of Person class: person1 and person2.
Does person1 have access to inspect and modify #secret field of person2?
class Person {
#secret;
constructor(secret) {
this.#secret = secret;
}
getSomebodyElsesSecret(somebody) {
return somebody.#secret;
}
setSomebodyElsesSecret(somebody, value) {
somebody.#secret = value;
}
}