TypeError: property "x" is non-configurable and can't be deleted
JavaScript 异常 "property is non-configurable and can't be deleted" 发生在尝试删除一个属性,但该属性是不可配置的。
消息
TypeError: Cannot delete property 'x' of #<Object> (V8-based) TypeError: property "x" is non-configurable and can't be deleted (Firefox) TypeError: Unable to delete property. (Safari)
错误类型
TypeError 仅在严格模式下。
哪里出错了?
尝试删除一个属性,但该属性是不可配置的。configurable 属性控制该属性是否可以从对象中删除,以及其属性(除了 writable 之外)是否可以更改。
此错误仅在严格模式代码中发生。在非严格模式代码中,该操作返回 false。
示例
尝试删除不可配置的属性
不可配置的属性不常见,但它们可以使用 Object.defineProperty() 或 Object.freeze() 创建。
js
"use strict";
const obj = Object.freeze({ name: "Elsa", score: 157 });
delete obj.score; // TypeError
js
"use strict";
const obj = {};
Object.defineProperty(obj, "foo", { value: 2, configurable: false });
delete obj.foo; // TypeError
js
"use strict";
const frozenArray = Object.freeze([0, 1, 2]);
frozenArray.pop(); // TypeError
JavaScript 中也有一些内置的不可配置属性。也许你尝试删除一个数学常数。
js
"use strict";
delete Math.PI; // TypeError