ES5 |何时使用null以及何时使用undefined [英] ES5 | When to use null and when to use undefined

查看:161
本文介绍了ES5 |何时使用null以及何时使用undefined的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript空值或未定义

null

因此不能被覆盖。

null is a reserved word but not a keyword.
Hence it can not be over-written.

未定义是可以覆盖的内置全局变量。这就是为什么您看到jQuery在其IIFE中重新定义它的原因。只是为了确保它不会被覆盖。

undefined is a built in global that can be over-written. This is why you see jQuery re-define it in its IIFE. Just to make sure it was not over-written.

在ES 5中指定何时使用每一个的技术区别是什么。

What it the technical distinction of when to use each as specified in ES 5.

I知道我已经看到浏览器根据浏览器将未创建的localStorage属性设置为null或未定义。

I know that I have seen browsers set an un-created localStorage property to either null or undefined depending upon the browser.

localStorage.not_defined === null // sometimes

localStorage.not_defined === undefined // sometimes

ES 5如何

ES5没有提供澄清:

ES5 provides not clarification:


8.1未定义类型未定义类型只有一个值,称为undefined。任何未分配值的变量都具有
未定义的值。

8.1 The Undefined Type The Undefined type has exactly one value, called undefined. Any variable that has not been assigned a value has the value undefined.

8.2 Null类型Null类型只有一个值,称为null。

8.2 The Null Type The Null type has exactly one value, called null.

http:/ /www.ecma-international.org/publications/standards/Ecma-262.htm

推荐答案

常识如下: undefined 是变量

null 是变量或属性的状态,其中

null however is a state of a variable or property which indicates that it has no value assigned.

某些方法,例如 getElement ... 显式返回 null 表示结果集为空。如果您的函数没有return语句,则隐式返回 undefined

Some methods like getElement... explicitely return null to indicate that the resultset is empty. If your function has no return statement, implicitely undefined is returned instead.

通常,始终分配,永不未定义

In general, always assign null and never undefined.

这篇关于ES5 |何时使用null以及何时使用undefined的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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