如何取消设置 JavaScript 变量? [英] How can I unset a JavaScript variable?

查看:44
本文介绍了如何取消设置 JavaScript 变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 JavaScript 中有一个全局变量(实际上是一个 window 属性,但我认为这并不重要)它已经由以前的脚本填充,但我不想要另一个脚本稍后运行以查看其值或什至已定义.

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.

我已经把 some_var = undefined 用于测试 typeof some_var == "undefined" 但我真的不认为这是正确的方法去做吧.

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.

你怎么看?

推荐答案

delete 运算符从对象中删除属性.它不能删除变量.所以问题的答案取决于全局变量或属性是如何定义的.

The delete operator removes a property from an object. It cannot remove a variable. So the answer to the question depends on how the global variable or property is defined.

(1)如果是用var创建的,则不能删除.

(1) If it is created with var, it cannot be deleted.

例如:

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) 如果创建时没有var,可以删除.

(2) If it is created without var, it can be deleted.

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

技术说明

1.使用 var

在这种情况下,参考 g_a 是在 ECMAScript 规范所称的可变环境"附加到当前作用域 - 这可能是在函数内部使用 var 的情况下的函数执行上下文(尽管当您考虑 let) 或在全局"的情况下代码将 VariableEnvironment 附加到全局对象(通常是 window).

Technical Explanation

1. Using 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).

VariableEnvironment 中的引用通常不可删除 - ECMAScript 10.5 详细解释了这一点,但足以说明除非您的代码在 eval 上下文中执行(大多数基于浏览器的开发控制台使用),则不能删除用 var 声明的变量.

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.

在不使用 var 关键字的情况下尝试为名称赋值时,JavaScript 会尝试在 ECMAScript 规范所称的LexicalEnvironment",主要区别在于LexicalEnvironments 是嵌套的 - 即 LexicalEnvironment 有一个父级(ECMAScript 规范称之为外部环境引用")并且当 JavaScript 无法在 LexicalEnvironment 中定位引用时em>,它在父 LexicalEnvironment 中查找(详见 10.3.110.2.2.1).顶级词法环境全局环境",并且绑定到全局对象,因为它的引用是全局对象的属性.因此,如果您尝试访问未在当前作用域或任何外部作用域中使用 var 关键字声明的名称,JavaScript 最终将获取 window 对象的属性以作为参考.正如我们之前了解到的,可以删除对象的属性.

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 LexicalEnvironments are nested - that is a LexicalEnvironment has a parent (what the ECMAScript spec calls "outer environment reference") and when JavaScript fails to locate the reference in a LexicalEnvironment, it looks in the parent LexicalEnvironment (as detailed in 10.3.1 and 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. 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. As we've learned before, properties on objects can be deleted.

  1. 重要的是要记住 var 声明是提升"的.- 即它们总是被认为发生在它们所在作用域的开头 - 尽管不是在 var 语句中可能完成的值初始化 - 它留在原处.所以在下面的代码中,a 是来自 VariableEnvironment 而不是 window 属性的引用,它的值将是 10在代码的末尾:

  1. It is important to remember that var declarations are "hoisted" - i.e. 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. 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:

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.

    这篇关于如何取消设置 JavaScript 变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆