语法错误: 私有字段不能被删除
消息
SyntaxError: Private fields can not be deleted (V8-based) SyntaxError: private fields can't be deleted (Firefox) SyntaxError: Cannot delete private field X (Safari)
错误类型
哪里出了问题?
代码尝试对对象或类的私有属性(字段或方法)进行 delete
操作。这是 JavaScript 禁止的——私有属性不能动态添加或删除。
示例
js
class MyClass {
#myPrivateField;
deleteIt() {
delete this.#myPrivateField; // SyntaxError: private fields can't be deleted
}
}
js
class MyClass {
#myPrivateMethod() {
}
#deleteIt() {
delete this.#myPrivateMethod; // SyntaxError: private fields can't be deleted
}
}