The MDN docs for private fields mention:
It is a syntax error to refer to # names from out of scope. It is also a syntax error to refer to private fields that were not declared before they were called, or to attempt to remove declared fields with delete.
Outside of private fields, I can define a function which returns a constructor which can then be invoked as a class:
function CtorCreator(...) {
const CtorBeingCreated = function Whatever(...) {...}
// programmatically manipulate CtorBeingCreated
return CtorBeingCreated
}
const Ctor = CtorCreator(...)
const instance = new Ctor();
In this I can programmatically define fields and methods based on arguments passed to the CtorCreator
function. For example
...
CtorBeingCreated.prototype[...] = ...
...
Can I also define private fields and methods that way? E.g. some way to allow me to do
...
addPrivateFieldToCtor(CtorBeingCreated, <field-name>, <initial-value>)
// or
addPrivateMethodToCtor(CtorBeingCreated, <method-name>, <method-body>) // method body can access <field-name> previously defined
...