javascript中未定义的类型是什么? [英] What is the type of undefined in javascript?

查看:146
本文介绍了javascript中未定义的类型是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能说出JavaScript中undefined的类型是什么吗? typeof(未定义)会返回什么?

Can some body tell what is the type of undefined in JavaScript? What will typeof(undefined) return?

推荐答案

未定义的内容是什么?

JavaScript中有未定义(类型),未定义(值)和未定义(变量).

In JavaScript there is Undefined (type), undefined (value) and undefined (variable).

类型:

未定义(类型)是内置的JavaScript类型.

Undefined (type) is a built-in JavaScript type.

值:

未定义(值)是原始值,是唯一的值 未定义的类型.任何尚未分配值的属性, 假定未定义的值. (ECMA 4.3.9和4.3.10).一个功能 没有return语句,或者具有空return的函数 语句返回未定义.未提供函数的值 参数未定义.

Undefined (value) is a primitive and is the sole value of the Undefined type. Any property that has not been assigned a value, assumes the undefined value. (ECMA 4.3.9 and 4.3.10). A function without a return statement, or a function with an empty return statement returns undefined. The value of an unsupplied function argument is undefined.

变量:

未定义(变量)是一个全局属性,其初始值为 未定义(值),因为它是全局属性,所以我们也可以访问它 作为变量.为了保持一致,我总是将其称为变量 在本文中.

Undefined (variable) is a global property whose initial value is undefined (value), Since its a global property we can also access it as a variable. For consistency I’m always going to call it a variable in this article.

无效区分:

Undefined不同于null,后者也是一个原始值 表示故意缺少值.唯一的相似之处 在undefined和null之间,它们都被强制转换为false.

Undefined is distinct from null which is also a primitive value representing the intentional absence of a value. The only similarity between undefined and null is they both coerce to false.

那么什么是ReferenceError?

ReferenceError表示检测到无效的参考值(ECMA 5 15.11.6.3)

A ReferenceError indicates that an invalid reference value has been detected (ECMA 5 15.11.6.3)

实际上,这意味着当JavaScript尝试获取无法解析的引用的值时,将引发ReferenceError.

In practical terms, this means a ReferenceError will be thrown when JavaScript attempts to get the value of an unresolvable reference.

请注意消息语法在浏览器之间如何变化.正如我们将看到的,这些消息中没有一个是特别有启发性的:

Note how the message syntax varies across browser. As we will see none of these messages is particularly enlightening:

alert(foo);
//FF/Chrome: foo is not defined
//IE: foo is undefined
//Safari: can't find variable foo

这篇关于javascript中未定义的类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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