在Java脚本中全局变量不清楚 [英] In Java Script Global Variable is not clear

查看:113
本文介绍了在Java脚本中全局变量不清楚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

我已经在Java脚本中声明了全局变量,我尝试使用以下代码清除,但不清楚.

正如我定义的全局变量.

var usno;

然后在设置值之前,我尝试了以下代码

删除window.usno;
usno =未定义;
usno = null;

但没有成功.

请对此提供帮助

谢谢
Basit.

Dear,

I have declare global variable in java script, i try to clear with following code but it not clear.

As i defined global variable.

var usno;

then before set value i tried following code

delete window.usno;
usno = undefined;
usno=null;

but no success.

Please help on this

Thanks
Basit.

推荐答案

简单.你不能.

var定义的变量在作用域中一直存在.由于您创建了全局"变量,因此它在范围内无处不在,因此它永远不会消失.

最好的办法就是将其设置为null.它不会删除变量,但会删除内容.

delete语句用于删除属性,而不是整个变量.
Simple. You can''t.

A variable defined with var exists for the entire time it''s in scope. Since you created a "global" variable, it''s in scope everywhere so it''ll never go away.

The best you can do is just set it to null. It won''t remove the variable, but the contents.

The delete statement is for removing properties, not an entire variable.


如解决方案1所述,您不能真正删除变量,并不是因为它不可能",而是因为整个概念在JavaScript中毫无意义.当您编写var usno时,将创建变量,该变量等于undefined.从某种意义上讲,它变得可访问,使用该表达式或语句的任何表达式或语句都不会产生词法错误未定义usno".在这里,已定义"的使用与未定义"的含义不同,后者被称为变量的值.

全局变量不能仅由于if始终在范围内而被删除".同时,可以再次使它变得不确定.
As Solution 1 explains, you cannot really delete a variable, and not because it is "impossible", but because the whole concept makes no meaning in JavaScript. When you write var usno, the variable is created and equal to undefined. It becomes accessible in that sense that any expression or statement using it won''t generate a lexical error "usno is not defined". Here "defined" is used in a different sense that "undefined", which is referred to as the value of the variable.

The global variable cannot be "deleted" just because if is always in scope. At the same time, it can be made undefined again.
var usno; // not it is underfined
usno = null; // not undefined
usno = 3; // not undefined
usno = undefined; // undefined again


我们将其设为未定义,但它仍然是现有变量,因为它以 lexical 的意义存在.

这是JavaScript的微妙时刻.将其视为仅逐行工作的纯解释器"是一种常见的误解.不,起初整个脚本是通过词法分析器来的.而且很容易通过词汇错误进行检查.在这种情况下,即使脚本的第一部分正确,也不会执行.即使是词法错误也可以作为异常捕获,但仅当您运行解释器本身时才可以.在我的文章中,我解释了如何: JavaScript计算器 [
我确实了解到,如果我们忘记了您奇怪的delete window.usno尝试,您实际上并没有尝试删除"该变量,因此,我对删除"的考虑更多地是对解决方案1的回答,而不是对您询问如何处理"的问题.清除".然后,请参阅我对问题的评论.

—SA


We made it undefined, but it remains existing variable, because it exists as such in lexical sense.

This is a delicate moment of JavaScript. It''s a common misconception to consider it as a "pure interpreter" which only works line by line. No, at first the whole script comes through lexical analyzer; and it is easy to check up by making a lexical error. In this case, even if first part of script is correct, it won''t be executed at all. Even the lexical errors can be caught as exceptions, but only when you run the interpreter itself. In my article, I explain how: JavaScript Calculator[^].

How is it related to "deletion" of the variable? The declared variable exists in a lexical scope and will exist as long as the scope exists. From the point of view of practice of programming, it means nothing. How a defined variable can prevent anything? It''s just the name, nothing else. It does not occupy any space which should be made free of it. One can use it at any moment for any different purpose.

I do understand that you did not really try to "delete" the variable, if we forget your weird delete window.usno attempt, so my considerations on "deletion" was more in response to Solution 1 than to your question where you asked "how to clear". Then see also my comment to the question.

—SA


这篇关于在Java脚本中全局变量不清楚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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