简体   繁体   English

在javascript /可能的javascript错误中测试未定义的变量?

[英]Testing undefined variables in javascript/ possible javascript bug?

So I have been working with javascript for a website I am designing, shocker I know, I was trying to find a way to test if a variable did not exist or wasn't defined. 所以我一直在使用javascript为我正在设计的网站,我知道,我试图找到一种方法来测试变量是否不存在或未定义。 After getting through this I think being undefined and not existing are two different things. 在完成这个之后,我认为未定义且不存在是两个不同的事情。 Also I think its highly unlikely I found a bug but maybe someone with a better understanding of Javascript can explain to me why the following code works the way it does. 此外,我认为我发现错误的可能性极小,但也许对Javascript有更好理解的人可以向我解释为什么下面的代码按照它的方式工作。

<script type="text/javascript">
var t1="";
var t2;
if (t1==undefined) {document.write("t1 is undefined");}
if (t2==undefined) {document.write("t2 is undefined");}
</script> 

The above code returns "t2 is undefined". 上面的代码返回“t2未定义”。

<script type="text/javascript">
var t1="";
if (t1==undefined) {document.write("t1 is undefined");}
if (t2==undefined) {document.write("t2 is undefined");}
</script> 

This second code crashes I believe. 我相信第二个代码崩溃了。 So in the first code t2 exists but is not defined? 所以在第一个代码中t2存在但未定义? and in the second code it needs to exist before it can be undefined? 并且在第二个代码中它需要存在才能被取消定义? I just figured that if I did not write "var t2;" 我只是想,如果我没有写“var t2;” then tested for it, it would be undefined. 然后测试它,它将是未定义的。 Hopefully I have explained this question enough. 希望我已经足够解释了这个问题。

It's not a bug. 这不是一个错误。 In fact, the typeof operator is the only place where you can use an undeclared variable without getting an error. 事实上, typeof运算符是唯一可以在不出错的情况下使用未声明变量的地方。

See my answer Internet Explorer: "console is not defined" Error for a detailed explanation 请参阅我的答案Internet Explorer:“未定义控制台”详细说明出错

edit: 编辑:

This is how it's defined in the specs : 这是它在规范中定义的方式:

The production UnaryExpression : typeof UnaryExpression is evaluated as follows: 1. Let val be the result of evaluating UnaryExpression. 生产UnaryExpression : typeof UnaryExpression的计算方法如下:1。令val为评估UnaryExpression的结果。
2. If Type(val) is Reference, then 2.如果Type(val)是Reference,那么
a. 一种。 If IsUnresolvableReference(val) is true, return "undefined". 如果IsUnresolvableReference(val)为true,则返回“undefined”。
... ...

Everywhere else, IsUnresolvableReference==true results in an error. 在其他地方, IsUnresolvableReference==true导致错误。

Well in your case if you have: 在你的情况下,如果你有:

var t1 = "";

You will declare an empty string so it is normal to be "defined"; 您将声明一个空字符串,因此“定义”是正常的;

Whilst by doing: 通过这样做:

var t2;

You are not defining it as any type of javascript object 您没有将其定义为任何类型的JavaScript对象

It seems that there's a confusing naming convention here; 似乎这里有一个令人困惑的命名约定; We can say in the first case, you define variable t2 itself but it does not have a value defined. 我们可以说在第一种情况下,您定义变量 t2本身但它没有定义 You can think the "undefined" property as the "value-undefined" to be more precise. 您可以将“未定义”属性视为“值未定义”更精确。

In the second case, you have t2 variable itself is NOT defined, thus you will get an error when you try to use it in code: "variable undefined", this error is because of a different reason but it is named similarly, making the situation a little confusing. 在第二种情况下,你有t2 变量本身是未定义的,因此当你尝试在代码中使用它时会出现错误:“变量未定义”,这个错误是因为一个不同的原因,但它被命名为相似,使得情况有点混乱。 Think this error as "variable-undefined" to be more precise. 将此错误视为“变量未定义”更精确。

I think you can just use the property "null" instead of the "undefined" in the first case to be clear. 我想你可以在第一种情况下使用属性“null”而不是“undefined”来清楚。

var t1="";
var t2;
if (t1==null) {document.write("t1 is null");} 
if (t2==null) {document.write("t2 is null");} 

and; 和;

var t1="";
if (t1==null) {document.write("t1 is null");} 
if (t2==null) {document.write("t2 is null");} // this line gives error because t2 variable is undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM