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 |
瀏覽器相容性
載入中…