是否可以删除使用const声明的变量?

时间:2022-12-06 21:47:42

Given

鉴于

const localName = "local_name";
delete localName; // true
console.log(localName); // "local_name"

Is it possible to delete a variable declared using const?

是否可以删除使用const声明的变量?

1 个解决方案

#1


6  

delete is used to delete properties from an object.

delete用于从对象中删除属性。

delete foo;

will try to delete the property foo from the global object. Declared variables can never be removed with delete (no matter if you use const, let or var), and there is no other way to remove a "variable" (binding) (see @T.J.'s comment for some more details).

将尝试从全局对象中删除属性foo。声明的变量不能用delete来删除(无论您使用const、let或var),也没有其他方法来删除“变量”(绑定)(参见@T.J)。关于更多细节的评论。

Related: How to unset a JavaScript variable?

相关的:如何取消JavaScript变量的设置?

#1


6  

delete is used to delete properties from an object.

delete用于从对象中删除属性。

delete foo;

will try to delete the property foo from the global object. Declared variables can never be removed with delete (no matter if you use const, let or var), and there is no other way to remove a "variable" (binding) (see @T.J.'s comment for some more details).

将尝试从全局对象中删除属性foo。声明的变量不能用delete来删除(无论您使用const、let或var),也没有其他方法来删除“变量”(绑定)(参见@T.J)。关于更多细节的评论。

Related: How to unset a JavaScript variable?

相关的:如何取消JavaScript变量的设置?