It seems it's not possible to delete #
private class fields. The syntax doesn't allow it.
class Example {
#privatefield = 'some_value';
deleteField() {
delete this.#privatefield;
}
}
Uncaught SyntaxError: Private fields can not be deleted
Does anyone know why? What is the rationale behind allowing deletion of normal properties but not of private class fields?
CodePudding user response:
Another thing to note is that you can't create a private field later; they must be declared in the class definition:
Note that ESnext provides private fields only as declared up-front in a field declaration; private fields cannot be created later, ad-hoc, through assigning to them, the way that normal properties can.
So if you were to delete a private field, you'd never be able to add it back, which could cause many errors.
Not every specific detail is written out for their decisions into making these design decisions.
CodePudding user response:
First, this is not a bug. MDN states,
It is a syntax error to refer to # names from outside of the class. It is also a syntax error to refer to private properties that were not declared in the class body, or to attempt to remove declared properties with delete.
As for why, here's what the ECMAScript 2022 spec has to say:
[...] Although Property Descriptors are not used for private elements, private fields behave similarly to non-configurable, non-enumerable, writable data properties, private methods behave similarly to non-configurable, non-enumerable, non-writable data properties, and private accessors behave similarly to non-configurable, non-enumerable accessor properties.
(permalink, emphasis added)
Note that private fields are non-configurable, and the spec says that non-configurable properties cannot be deleted:
In addition, if a delete operator occurs within strict mode code and the property to be deleted has the attribute
{ [[Configurable]]: false }
(or otherwise cannot be deleted), a TypeError exception is thrown.
(permalink see Note 1)
Note that strict mode is enabled in all classes, and because private fields are only accessible inside classes, all references to private fields are in strict mode and thus cannot be deleted.
Normally, attempting to delete non-configurables results in a TypeError
, but the spec/engine chose to use a SyntaxError
instead for private fields. This makes more sense because they are defined as non-configurable in the syntax (and, as the spec stated, they don't actually have the [[Configurable]]
attribute; they just act like it).
CodePudding user response:
The main reason private class fields cannot be deleted is because they are intended to be immutable. Private class fields are used to encapsulate implementation details and should not be modified or deleted once set. By not allowing deletion of private class fields, it ensures that the implementation remains consistent and prevents unexpected behavior or bugs that may arise from modifying the internal state of the class. Additionally, it also aligns with the intended use of private class fields, which is to provide a way to define hidden state that is not exposed to the outside world.