我在JavaScript中有一个全局变量(实际上是一个window
属性,但我不认为这很重要),该变量已经由先前的脚本填充,但是我不希望另一个脚本稍后运行以查看其值,甚至定义。
我已经说过了some_var = undefined
,它可以用于测试目的,typeof some_var == "undefined"
但是我真的不认为这是正确的方法。
你怎么看?
我在JavaScript中有一个全局变量(实际上是一个window
属性,但我不认为这很重要),该变量已经由先前的脚本填充,但是我不希望另一个脚本稍后运行以查看其值,甚至定义。
我已经说过了some_var = undefined
,它可以用于测试目的,typeof some_var == "undefined"
但是我真的不认为这是正确的方法。
你怎么看?
You cannot delete a variable if you declared it (with var x;) at the time of first use. However, if your variable x first appeared in the script without a declaration, then you can use the delete operator (delete x;) and your variable will be deleted, very similar to deleting an element of an array or deleting a property of an object.
In addition to what everyone had written, also note that delete
returns boolean. It can tell you if the delete was successful or not.
Testing on Chrome, everything except let
was deleltable. when delete
returned true
it actually removed them:
implicit_global = 1;
window.explicit_global = 1;
function_set = function() {};
function function_dec() { };
var declared_variable = 1;
let let_variable = 1;
delete delete implicit_global; // true, tested on Chrome 52
delete window.explicit_global; // true, tested on Chrome 52
delete function_set; // true, tested on Chrome 52
delete function_dec; // true, tested on Chrome 52
delete declared_variable; // true, tested on Chrome 52
delete let_variable; // false, tested on Chrome 78
Variables, in contrast with simple properties, have attribute [[Configurable]], meaning impossibility to remove a variable via the delete operator. However there is one execution context on which this rule does not affect. It is the eval context: there [[Configurable]] attribute is not set for variables.
TLDR:简单定义的变量(无var
,let
,const
)可以与被删除delete
。如果使用var
,let
,const
-他们不能既不删除delete
也不符合Reflect.deleteProperty
。
Chrome 55:
simpleVar = "1";
"1"
delete simpleVar;
true
simpleVar;
VM439:1 Uncaught ReferenceError: simpleVar is not defined
at <anonymous>:1:1
(anonymous) @ VM439:1
var varVar = "1";
undefined
delete varVar;
false
varVar;
"1"
let letVar = "1";
undefined
delete letVar;
true
letVar;
"1"
const constVar="1";
undefined
delete constVar;
true
constVar;
"1"
Reflect.deleteProperty (window, "constVar");
true
constVar;
"1"
Reflect.deleteProperty (window, "varVar");
false
varVar;
"1"
Reflect.deleteProperty (window, "letVar");
true
letVar;
"1"
FF Nightly 53.0a1显示相同的行为。
some_var = null;
//or remove it..
delete some_var;
@scunlife的答案会起作用,但从技术上讲应该是
delete window.some_var;
当目标不是对象属性时,delete应该被禁止使用。例如,
(function() {
var foo = 123;
delete foo; // wont do anything, foo is still 123
var bar = { foo: 123 };
delete bar.foo; // foo is gone
}());
但是由于全局变量实际上是window对象的成员,因此它可以工作。
当涉及原型链时,使用delete会变得更加复杂,因为它只会从目标对象中移除属性,而不会从原型中移除属性。例如,
function Foo() {}
Foo.prototype = { bar: 123 };
var foo = new Foo();
// foo.bar is 123
foo.bar = 456;
// foo.bar is now 456
delete foo.bar;
// foo.bar is 123 again.
所以要小心
编辑:我的回答有点不准确(请参阅“误解”在最后)。该链接说明了所有细节,但摘要是,浏览器之间以及要删除的对象之间可能会有很大差异。delete object.someProp
一般只要保持安全object !== window
。var
尽管您可以在适当的情况下使用,但我仍然不会使用它来删除声明的变量。
I am bit confused. If all you are wanting is for a variables value to not pass to another script then there is no need to delete the variable from the scope. Simply nullify the variable then explicit check if it is or is not null. Why go through the trouble of deleting the variable from scope? What purpose does this server that nullifying can not?