TypeError: property "x" is non-configurable and can't be deleted
消息
TypeError: property "x" is non-configurable and can't be deleted. (Firefox) TypeError: Cannot delete property 'x' of #<Object> (Chrome)
错误类型
TypeError
只出现在严格模式下。
哪里有问题?
示例
不可配置的属性并不特别常见,但是它们可以使用 Object.defineProperty()
或 Object.freeze()
创建。
js
"use strict";
var obj = Object.freeze({ name: "Elsa", score: 157 });
delete obj.score; // TypeError
("use strict");
var obj = {};
Object.defineProperty(obj, "foo", { value: 2, configurable: false });
delete obj.foo; // TypeError
("use strict");
var frozenArray = Object.freeze([0, 1, 2]);
frozenArray.pop(); // TypeError
也有一些内建于 JavaScript 的不可配置属性。你可能会尝试删除一个数学常量。
js
"use strict";
delete Math.PI; // TypeError