This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Description
I was writing a simple object today and I wondered why private fields are restricted to classes?
e.g.
function make(name, ssnP) {
return {
name,
validateUser(expectedSSN) {
return ssnP === expectedSSN;
},
};
}
This works by closing over the ssn, but it seems that using the private-fields syntax we could make it much more explicit and not require any closure to be created.
function make(name, ssnP) {
return {
name,
#ssn: ssnP,
validateUser(expectedSSN) {
return this.#ssn === expectedSSN;
},
};
}
However, if I understand the proposal correctly, this wouldn't be allowed because the object isn't defined using 'class'.
I'm curious why this is?