The delete
operator removes a property from an object.(delete
运算符从对象中删除属性。)
It cannot remove a variable.(它不能删除变量。) So the answer to the question depends on how the global variable or property is defined.(因此,问题的答案取决于如何定义全局变量或属性。)
(1) If it is created with var
, it cannot be deleted.((1)如果使用var
创建,则无法删除。)
For example:(例如:)
var g_a = 1; //create with var, g_a is a variable
delete g_a; //return false
console.log(g_a); //g_a is still 1
(2) If it is created without var
, it can be deleted.((2)如果不使用var
创建它,则可以将其删除。)
g_b = 1; //create without var, g_b is a property
delete g_b; //return true
console.log(g_b); //error, g_b is not defined
Technical Explanation(技术说明)
1. Using var
(1.使用var
)
In this case the reference g_a
is created in what the ECMAScript spec calls " VariableEnvironment " that is attached to the current scope - this may be the a function execution context in the case of using var
inside a function (though it may be get a little more complicated when you consider let
) or in the case of "global" code the VariableEnvironment is attached to the global object (often window
).(在这种情况下,引用g_a
是在ECMAScript规范称为“ VariableEnvironment ”的情况下创建的,该引用附加到当前作用域-在函数内部使用var
的情况下,这可能是函数执行上下文(尽管可能会得到一些帮助)当您考虑let
)或在“全局”代码的情况下, 变量环境更加复杂, 变量环境被附加到全局对象(通常是window
)。)
References in the VariableEnvironment are not normally deletable - the process detailed in ECMAScript 10.5 explains this in detail, but suffice it to say that unless your code is executed in an eval
context (which most browser-based development consoles use), then variables declared with var
cannot be deleted.(VariableEnvironment中的引用通常是不可删除的-ECMAScript 10.5中详细介绍的过程对此进行了详细说明,但可以这样说,除非您的代码在eval
上下文中执行(大多数基于浏览器的开发控制台都使用该上下文),然后使用var
无法删除。)
2. Without Using var
(2.不使用var
)
When trying to assign a value to a name without using the var
keyword, Javascript tries to locate the named reference in what the ECMAScript spec calls " LexicalEnvironment ", and the main difference is that LexicalEvironment s are nested - that is a LexicalEnvironment has a parent (what the ECMAScript spec calls "outer environment reference") and when Javscript fails to locate the reference in a LexicalEenvironment , it looks in the parent LexicalEnvironment (as detailed in 10.3.1 and 10.2.2.1 ).(当尝试在不使用var
关键字的情况下为名称分配值时,Javascript尝试在ECMAScript规范称为“ LexicalEnvironment ”的位置查找命名的引用,主要区别在于LexicalEvironment是嵌套的-LexicalEnvironment有一个父项(ECMAScript规范称为“外部环境引用”),并且当Javscript无法在LexicalEenvironment中找到引用时,它将在父LexicalEnvironment中查找(如10.3.1和10.2.2.1中所述 )。)
The top level LexicalEnvironment is the " global environment ", and that is bound to the global object in that its references are the global object's properties.(顶级LexicalEnvironment是“ 全局环境 ”,它绑定到全局对象,因为它的引用是全局对象的属性。) So if you try to access a name that was not declared using a var
keyword in the current scope or any outer scopes, Javascript will eventually fetch a property of the window
object to serve as that reference.(因此,如果您尝试访问在当前范围或任何外部范围中未使用var
关键字声明的名称,则Javascript最终将获取window
对象的属性以用作该引用。) As we've learned before, properties on objects can be deleted.(正如我们之前所了解的,可以删除对象的属性。)
Notes(笔记)
It is important to remember that var
declarations are "hoisted" - ie they are always considered to have happened in the beginning of the scope that they are in - though not the value initialization that may be done in a var
statement - that is left where it is.(重要的是要记住var
声明是“悬挂的”-即始终认为它们已在它们所在范围的开头发生-尽管不是在var
语句中可能完成的值初始化-留在了它是。)
So in the following code, a
is a reference from the VariableEnvironment and not the window
property and its value will be 10
at the end of the code:(因此,在以下代码中, a
是VariableEnvironment的引用,而不是window
属性的引用,在代码末尾,其值为10
:) function test() { a = 5; var a = 10; }
The above discussion is when "strict mode" is not enabled.(以上讨论是在未启用“严格模式”的情况下。)
Lookup rules are a bit different when using "strict mode" and lexical references that would have resolved to window properties without "strict mode" will raise "undeclared variable" errors under "strict mode".(使用“严格模式”时,查找规则略有不同,如果没有“严格模式”就可以解析为窗口属性的词法引用将在“严格模式”下引发“未声明变量”错误。) I didn't really understand where this is specified, but its how browsers behave.(我不太了解在哪里指定它,但是它在浏览器中的表现如何。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…