Javascript在'undefined'和'not defined'之间有什么区别? [英] What is the difference in Javascript between 'undefined' and 'not defined'?

查看:259
本文介绍了Javascript在'undefined'和'not defined'之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果您尝试使用不存在且尚未声明的变量,则javascript将引发错误。 var name未定义,脚本将在那里停止。

If you attempt to use a variable that does not exist and has not been declared, javascript will throw an error. var name is not defined, and the script will stop then and there.

但如果你使用<检查它code> typeof noname 然后它将返回undefined。我一直认为你必须声明一个var才能拥有一个类型。通过使用:var a;因此它具有未定义的类型或赋予它类似的值: var b = 5; 所以它具有其值的类型。

But if you check it using typeof noname then it will return "undefined". I always thought that you had to declare a var for it to have a type. Either by using: var a; so it has the type of undefined or by giving it a value like: var b =5; so it has the type of its value.

var a; var b = undefined;

推荐答案

未声明的变量(即不存在的变量)没有类型 - 因此其类型为 未定义。我相信普遍接受的测试方法是否未定义的方法是

An undeclared variable (that is, one that doesn't exist) does not have a type - and so its type is undefined. I believe that the generally accepted way to test if something is undefined is

typeof var === 'undefined'

而不是

var === undefined

因为如果变量不存在,则无法访问它。如果您确定变量应该存在,后一种情况可能会更有用,因为两者之间的差异是未声明的变量在第一种情况下将返回false但在第二种情况下会导致错误。

since if the variable does not exist, you cannot access it. The latter case might be more useful if you are sure the variable should exist, since the difference between the two is that an undeclared variable will return false in the first case but cause an error in the second case.

如果您使用的是第二种变体,请确保使用三等号运算符; (更常见的)双等于运算符执行类型强制,因此 null == undefined 为真,而 null === undefined 是假的。有时您可能想要第一个行为,但通常您会想要第二个行为,特别是如果您正在测试未定义的行为,因此识别差异非常重要。 (这是第一种情况的另一个好处,因为它不可能产生这种微妙的错误。)

Make sure that you use the triple-equals operator though if you're using the second variant; the (more usual) double-equals operator performs type coercion, so null == undefined is true while null === undefined is false. Sometimes you might want the first behaviour, though often you'll want the second, especially if you're testing against undefined, so it's important to recognise the difference. (This is another benefit of the first case since it's not possible to make this subtle error).

是的,变量可以具有未定义的值,并且您可以显式赋值给他们。将 undefined 分配给变量虽然可能令人困惑,因为它有点悖论(你已经将变量定义为未定义),并且无法将该变量与不存在的变量或未初始化的变量。如果要表示变量当前没有值,或者如果要完全取消声明变量,请使用null,使用 delete {varname} 。将undefined赋值给变量不会删除该变量的声明;如果你遍历它们,它仍然会出现在它拥有对象的属性列表中,所以我想不出任何会对你有益的情况。

Yes, variables can have a value of undefined and you can explicitly assign values to them. Assigning undefined to a variable though is probably confusing, since it's a bit of a paradox (you've defined the variable as undefined) and it's not possible to distinguish that variable from either variables that don't exist or uninitialised variables. Use null if you want to signify that a variable has no value currently, or if you want to "undeclare" the variable altogether, use delete {varname}. Assigning undefined to a variable does not remove that variable's declaration; and it will still appear in the list of its owning object's properties if you iterate over them, so I can't think of any situation this would benefit you.

编辑:在回应注释时,在将值与===(或==)进行比较时,必须引用变量以获取其当前值以进行比较。由于该变量不存在,因此该解除引用失败(到目前为止没有真正的意外)。虽然您可能希望typeof运算符以类似的方式工作(获取当前值,查看其类型),但它特别适用于未定义的变量。 简短版本(由 Mozilla参考使用)只是typeof运算符返回一个字符串,表示未评估的操作数的类型。

In response to the comment, when comparing the values with === (or ==), the variable must be deferenced to obtain its current value in order to do the comparison. Since the variable doesn't exist, this dereferencing fails (no real surprises so far). While you might expect the typeof operator to work in a similar way (get the current value, see what its type is) it specifically works with undefined variables. The "short version" (as used by the Mozilla reference ) is simply that "The typeof operator returns a string indicating the type of the unevaluated operand."

长版本,从 ECMAScript规范,是未定义变量的特例:

The long version, extracted from the ECMAScript spec, is that there's a special case for undefined variables:


11.4.3 typeof UnaryExpression 评估如下:


  1. 评估 UnaryExpression (根据10.1.4,它返回类型为Reference的值,其基础对象为 null ,其属性名称为标识符,如果变量未声明)。

  2. 如果Type(Result(1))不是Reference,请转到步骤4.(这是参考)

  3. 如果GetBase(结果( 1)) null ,返回undefined 。 (这是匹配未定义变量的特殊情况)

  1. Evaluate UnaryExpression (as per 10.1.4 this returns "a value of type Reference whose base object is null and whose property name is the identifier" if the variable is undeclared).
  2. If Type(Result(1)) is not Reference, go to step 4. (It is a Reference)
  3. If GetBase(Result(1)) is null, return "undefined". (This is the special case matching undefined variables)


至于您对第一个问题的评论, 你怎么能分类不存在的东西 - 这很简单!简单地将所有概念划分为存在的东西(例如松鼠,岩石)和不存在的东西(独角兽,翘曲驱动器)。这就是未定义的意思;无论它的英文语义是什么,它的Javascript含义是该变量尚未被声明或填充,因此当一个名为foo的变量的概念有效时,不存在变量该名称持有实际价值。

As for your comment to the first question, "how can you classify what does not exist" - it's easy! Simply divide all concepts into things that do exist (e.g. squirrel, rock) and those that don't exist (unicorns, warp drives). That's what undefined means; regardless of its semantics in English, its Javascript meaning is that the variable hasn't been declared or populated yet, and so while the concept of "a variable called foo" is valid, there exists no variable with that name holding a real value.

这篇关于Javascript在'undefined'和'not defined'之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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