Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

javascript - 如何取消设置JavaScript变量?(How to unset a JavaScript variable?)

I have a global variable in JavaScript (actually a window property, but I don't think it matters) which was already populated by a previous script but I don't want another script that will run later to see its value or that it was even defined.(我在JavaScript中有一个全局变量(实际上是一个window属性,但我认为这并不重要),该变量已经由先前的脚本填充,但是我不希望另一个稍后运行的脚本来查看其值或甚至定义。)

I've put some_var = undefined and it works for the purpose of testing typeof some_var == "undefined" but I really do not think it's the right way to go about it.(我已经放了some_var = undefined ,它可以用于测试typeof some_var == "undefined"但是我真的不认为这是正确的方法。)

What do you think?(你怎么看?)

  ask by Guss translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.110.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(笔记)

  1. 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:(因此,在以下代码中, aVariableEnvironment的引用,而不是window属性的引用,在代码末尾,其值为10 :)

    function test() { a = 5; var a = 10; }

  2. 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.(我不太了解在哪里指定它,但是它在浏览器中的表现如何。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...