SyntaxError: applying the 'delete' operator to an unqualified name is deprecated
JavaScript 嚴格模式下才會出現的異常“對不合格名稱應用 'delete' 運算子已棄用”,此異常發生在嘗試使用 delete 運算子刪除變數時。
訊息
SyntaxError: Delete of an unqualified identifier in strict mode. (V8-based) SyntaxError: applying the 'delete' operator to an unqualified name is deprecated (Firefox) SyntaxError: Cannot delete unqualified property 'a' in strict mode. (Safari)
錯誤型別
SyntaxError 僅在嚴格模式下出現。
哪裡出錯了?
JavaScript 中的普通變數不能使用 delete 運算子刪除。在嚴格模式下,嘗試刪除變數將丟擲錯誤,並且不允許進行此操作。
delete 運算子只能刪除物件的屬性。如果物件屬性是可配置的,則它們是“合格的”。
與普遍的看法相反,delete 運算子與直接釋放記憶體沒有任何關係。記憶體管理是透過斷開引用間接完成的,請參閱記憶體管理頁面和 delete 運算子頁面瞭解更多詳細資訊。
此錯誤僅發生在嚴格模式程式碼中。在非嚴格程式碼中,此操作僅返回 false。
示例
釋放變數內容
在嚴格模式下嘗試刪除普通變數會丟擲錯誤
js
"use strict";
var x;
// …
delete x;
// SyntaxError: applying the 'delete' operator to an unqualified name
// is deprecated
要釋放變數的內容,可以將其設定為 null
js
"use strict";
var x;
// …
x = null;
// x can be garbage collected