delete
delete 运算符用于删除对象的属性。如果属性的值是一个对象,并且没有其他引用指向该对象,则该属性持有的对象最终会被自动释放。
试一试
const employee = {
firstName: "Maria",
lastName: "Sanchez",
};
console.log(employee.firstName);
// Expected output: "Maria"
delete employee.firstName;
console.log(employee.firstName);
// Expected output: undefined
语法
delete object.property
delete object[property]
注意: 语法允许 delete 运算符后接更广泛的表达式,但只有上述形式会产生有意义的行为。
参数
object-
一个对象的名称,或求值结果为对象的表达式。
property(属性)-
要删除的属性。
返回值
异常
TypeError-
如果属性是自有不可配置属性,则在严格模式下抛出此错误。
ReferenceError-
如果
object是super,则抛出此错误。
描述
delete 运算符与其他一元运算符(例如 typeof)具有相同的优先级。因此,它接受由更高优先级运算符形成的任何表达式。然而,以下形式在严格模式下会导致早期语法错误
delete identifier;
delete object.#privateProperty;
由于类自动处于严格模式,并且私有元素只能在类体中合法引用,这意味着私有元素永远不能被删除。虽然如果 identifier 指向全局对象的可配置属性,delete identifier 可能会奏效,但你应该避免这种形式,并改用 globalThis 前缀。
虽然接受其他表达式,但它们不会产生有意义的行为
delete console.log(1);
// Logs 1, returns true, but nothing deleted
delete 运算符从对象中删除给定属性。如果成功删除,它将返回 true,否则返回 false。与普遍认为的(可能由于 C++ 中的 delete 等其他编程语言)相反,delete 运算符与直接释放内存无关。内存管理通过打破引用间接完成。有关更多详细信息,请参阅内存管理页面。
考虑以下场景很重要
- 如果你尝试删除的属性不存在,
delete不会产生任何效果并返回true。 delete只对自有属性有效。如果对象的原型链上存在同名属性,则删除后,对象将使用原型链上的属性。- 不可配置的属性不能删除。这包括内置对象的属性,例如
Math、Array、Object,以及使用Object.defineProperty()等方法创建为不可配置的属性。 - 删除变量(包括函数参数)永远不起作用。
delete variable在严格模式下会抛出SyntaxError,在非严格模式下则没有效果。
示例
使用 delete
注意: 以下示例仅使用非严格模式功能,例如隐式创建全局变量和删除标识符,这些在严格模式下是被禁止的。
// Creates the property empCount on the global scope.
// Since we are using var, this is marked as non-configurable.
var empCount = 43;
// Creates the property EmployeeDetails on the global scope.
// Since it was defined without "var", it is marked configurable.
EmployeeDetails = {
name: "xyz",
age: 5,
designation: "Developer",
};
// delete can be used to remove properties from objects.
delete EmployeeDetails.name; // returns true
// Even when the property does not exist, delete returns "true".
delete EmployeeDetails.salary; // returns true
// EmployeeDetails is a property of the global scope.
delete EmployeeDetails; // returns true
// On the contrary, empCount is not configurable
// since var was used.
delete empCount; // returns false
// delete also does not affect built-in static properties
// that are non-configurable.
delete Math.PI; // returns false
function f() {
var z = 44;
// delete doesn't affect local variable names
delete z; // returns false
}
delete 和原型链
在以下示例中,我们删除了对象的自有属性,而原型链上存在同名属性
function Foo() {
this.bar = 10;
}
Foo.prototype.bar = 42;
const foo = new Foo();
// foo.bar is associated with the
// own property.
console.log(foo.bar); // 10
// Delete the own property within the
// foo object.
delete foo.bar; // returns true
// foo.bar is still available in the
// prototype chain.
console.log(foo.bar); // 42
// Delete the property on the prototype.
delete Foo.prototype.bar; // returns true
// The "bar" property can no longer be
// inherited from Foo since it has been
// deleted.
console.log(foo.bar); // undefined
删除数组元素
当你删除数组元素时,数组的 length 不受影响。即使你删除数组的最后一个元素也是如此。
当 delete 运算符删除数组元素时,该元素将不再存在于数组中。在以下示例中,trees[3] 通过 delete 被删除。
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
delete trees[3];
console.log(3 in trees); // false
这将创建一个带有空槽的稀疏数组。如果你希望数组元素存在但其值为 undefined,请使用 undefined 值而不是 delete 运算符。在以下示例中,trees[3] 被赋值为 undefined,但数组元素仍然存在
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees[3] = undefined;
console.log(3 in trees); // true
相反,如果你想通过改变数组内容来删除数组元素,请使用 splice() 方法。在以下示例中,trees[3] 使用 splice() 从数组中完全删除
const trees = ["redwood", "bay", "cedar", "oak", "maple"];
trees.splice(3, 1);
console.log(trees); // ["redwood", "bay", "cedar", "maple"]
删除不可配置的属性
当属性被标记为不可配置时,delete 不会产生任何效果,并返回 false。在严格模式下,这将引发 TypeError。
const Employee = {};
Object.defineProperty(Employee, "name", { configurable: false });
console.log(delete Employee.name); // returns false
var 创建不可配置的属性,不能使用 delete 运算符删除
// Since "nameOther" is added using with the
// var keyword, it is marked as non-configurable
var nameOther = "XYZ";
// We can access this global property using:
Object.getOwnPropertyDescriptor(globalThis, "nameOther");
// {
// value: "XYZ",
// writable: true,
// enumerable: true,
// configurable: false
// }
delete globalThis.nameOther; // return false
在严格模式下,这将引发异常。
删除全局属性
如果全局属性是可配置的(例如,通过直接属性赋值),则可以将其删除,随后将其作为全局变量引用将产生 ReferenceError。
globalThis.globalVar = 1;
console.log(globalVar); // 1
// In non-strict mode, you can use `delete globalVar` as well
delete globalThis.globalVar;
console.log(globalVar); // ReferenceError: globalVar is not defined
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-delete-operator |
浏览器兼容性
加载中…