语法错误:不推荐将 'delete' 运算符应用于非限定名称
当尝试使用 delete
运算符删除变量时,会发生 JavaScript 严格模式-only 异常 "不推荐将 'delete' 运算符应用于非限定名称"。
¥The JavaScript strict mode-only exception "applying the 'delete' operator to an unqualified name is deprecated" occurs when variables are attempted to be deleted using the delete
operator.
信息
错误类型
什么地方出了错?
¥What went wrong?
JavaScript 中的普通变量无法使用 delete
运算符删除。在严格模式下,尝试删除变量将引发错误并且是不允许的。
¥Normal variables in JavaScript can't be deleted using the delete
operator. In strict mode, an attempt to delete a variable will throw an error and is not allowed.
delete
运算符只能删除对象的属性。如果对象属性是可配置的,则为 "qualified"。
¥The delete
operator can only delete properties on an object. Object properties are "qualified" if they are configurable.
与普遍看法不同,delete
运算符与直接释放内存无关。内存管理是通过中断引用间接完成的,请参阅 内存管理 页面和 delete
操作符页面了解更多详细信息。
¥Unlike what common belief suggests, the delete
operator has nothing to do with directly freeing memory. Memory management is done indirectly via breaking references, see the memory management page and the delete
operator page for more details.
此错误仅发生在 严格模式代码 中。在非严格代码中,该操作仅返回 false
。
¥This error only happens in strict mode code. In non-strict code, the operation just returns false
.
示例
释放变量的内容
¥Freeing the contents of a variable
尝试删除普通变量会在严格模式下引发错误:
¥Attempting to delete a plain variable throws an error in strict mode:
"use strict";
var x;
// …
delete x;
// SyntaxError: applying the 'delete' operator to an unqualified name
// is deprecated
要释放变量的内容,可以将其设置为 null
:
¥To free the contents of a variable, you can set it to null
:
"use strict";
var x;
// …
x = null;
// x can be garbage collected
也可以看看
¥See also