首页 > 其他 > 详细

如何区分null和undefined

时间:2018-11-15 13:28:05      阅读:133      评论:0      收藏:0      [点我收藏+]

null和undefined是两种数据类型, 如果硬要区分的话。

null是一种类型, 赋值变量为null型。

未定义的变量, 即为undefined。

var a = null
a  // null
var b
b  // undefined
typeof(b)  // "undefined"

标准解释

6.1.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.

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

undefined使用场景:

  • 判断变量, 判断函数传参异常, 判断对象字段和方法是否存在(或者正确使用Object.propretype.call(ob, ‘key‘))
  • es6中应对eslint规范的export default undefined

如何区分null和undefined

原文:https://www.cnblogs.com/daaasheng/p/9962668.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!