为什么null在JavaScript中被认为是一个对象?

正在检查

if ( object == null )
      Do something

if ( !object )
      Do something

?

还有:

null和undefined之间的区别是什么?


当前回答

理解null和undefined的一种方法是了解它们出现的位置。

在以下情况下期望返回空值:

查询DOM的方法 console.log (window.document.getElementById(“nonExistentElement”)); / /输出:零 从Ajax请求接收到的JSON响应


    {
      name: "Bob",
      address: null
    }

RegEx.exec。 新功能处于不断变化的状态。下面返回null:


        var proto = Object.getPrototypeOf(Object.getPrototypeOf({}));

       // But this returns undefined:

        Object.getOwnPropertyDescriptor({}, "a");

所有其他不存在的情况都用undefined表示(由@Axel指出)。以下每一个都打印为“undefined”:

    var uninitalised;
    console.log(uninitalised);

    var obj = {};
    console.log(obj.nonExistent);

    function missingParam(missing){
        console.log(missing);
    }

    missingParam();

    var arr = [];
    console.log(arr.pop());        

当然,如果你决定写var unitialized = null;或者你自己从一个方法中返回null,那么你在其他情况下也会出现null。但这应该是很明显的。

第三种情况是当你想访问一个变量,但你甚至不知道它是否已经声明。在这种情况下,使用typeof来避免引用错误:

if(typeof unknown !== "undefined"){
    //use unknown
}

总之,在操作DOM、处理Ajax或使用某些ECMAScript 5特性时检查是否为空。对于所有其他情况,检查undefined严格相等是安全的:

if(value === undefined){
  // stuff
}

其他回答

看看这个:

   <script>
function f(a){
  alert(typeof(a));
  if (a==null) alert('null');
  a?alert(true):alert(false);
}
</script>
                                          //return:
<button onclick="f()">nothing</button>    //undefined    null    false
<button onclick="f(null)">null</button>   //object       null    false
<button onclick="f('')">empty</button>    //string               false
<button onclick="f(0)">zero</button>      //number               false
<button onclick="f(1)">int</button>       //number               true
<button onclick="f('x')">str</button>     //string               true
typeof null;      // object
typeof undefined; // undefined

null值表示有意不存在任何对象值。它是JavaScript的基本值之一,在布尔操作中被视为假值。

var x = null;
var y;

X被声明并定义为null

Y声明了,但没有定义。它声明时没有值,所以没有定义。

Z没有被声明,所以如果你试图使用Z,它也是未定义的。

什么是类型?

类型是对值进行分类的一种方式。下面是一个包含有问题的类型及其typeof结果的表格。

Type Values type contains typeof result Is typeof result a lie?
Undefined Only: undefined "undefined" No
Null Only: null "object" Yes
Object Infinite amount of values: {}, {a: "b"}, ... "object" No

null不是一个对象,它是一个null类型的值。

typeof操作符在说谎!它返回“object”为空在JavaScript语言中是一个错误。

我在我的开源电子书中写了一章。你可以在这里阅读https://github.com/carltheperson/advanced-js-objects

null和undefined的主要区别在于null表示 一个缺失的对象,而undefined表示变量的未初始化状态。

你可以认为null是一个未定义的对象,但未定义只是未定义的 因为它的类型没有定义。

let a; 
console.log(a); //undefined, since it is declared but not initialized

console.log(null == undefined) //true
console.log(null === undefined) // false

console.log(typeof null) //object
console.log(typeof undefined) //undefined

差异可以总结为以下代码片段:

alert(typeof(null));      // object
alert(typeof(undefined)); // undefined

alert(null !== undefined) //true
alert(null == undefined)  //true

检查

Object == null与检查if (! Object)不同。

后者等于!布尔(对象),因为一元!运算符自动将右操作数转换为布尔型。

因为布尔(null)等于false,那么!false === true。

因此,如果对象不是null,而是false或0或"",检查将通过 因为:

alert(Boolean(null)) //false
alert(Boolean(0))    //false
alert(Boolean(""))   //false