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

正在检查

if ( object == null )
      Do something

if ( !object )
      Do something

?

还有:

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


当前回答

下面的函数说明了原因,并能够计算出差异:

function test() {
        var myObj = {};
        console.log(myObj.myProperty);
        myObj.myProperty = null;
        console.log(myObj.myProperty);
}

如果你打电话

test();

你要

未定义的 零

第一个console.log(…)尝试在myObj尚未定义时从myProperty获取myProperty -因此它返回“未定义”。在将其赋值为null后,第二个console.log(…)显然返回“null”,因为myProperty存在,但它的值为null。

为了能够查询这种差异,JavaScript有null和undefined:而null就像在其他语言中是一个对象一样,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
(name is undefined)

你:你叫什么名字?(*) JavaScript:名字?名字是什么?我不知道你在说什么。你从来没提过任何名字。你在(客户端)看到其他脚本语言了吗?

name = null;

你:你叫什么名字? JavaScript:我不知道。

简而言之,未定义是指事物没有概念存在;它没有类型,在此范围内从未被引用过;Null是已知对象存在的位置,但不知道值是多少。

需要记住的一件事是,null在概念上与false或""或诸如此类的不一样,即使它们在类型转换后相等,即。

name = false;

你:你叫什么名字? 布尔值为false。

name = '';

你:你叫什么名字? JavaScript:空字符串


*: name在此上下文中的意思是一个从未定义过的变量。它可以是任何未定义的变量,然而,name是几乎任何HTML表单元素的属性。它可以追溯到很久以前,在id之前就设立了。它很有用,因为id必须是唯一的,但名称不必是唯一的。

思考“null”的最佳方式是回忆一下类似的概念是如何在数据库中使用的,在数据库中它表示字段包含“根本没有值”。

是的,物品的价值是已知的;它是“被定义的”。已经初始化。 该项的值是:“没有值。”

This is a very useful technique for writing programs that are more-easily debugged. An 'undefined' variable might be the result of a bug ... (how would you know?) ... but if the variable contains the value 'null,' you know that "someone, somewhere in this program, set it to 'null.'" Therefore, I suggest that, when you need to get rid of the value of a variable, don't "delete" ... set it to 'null.' The old value will be orphaned and soon will be garbage-collected; the new value is, "there is no value (now)." In both cases, the variable's state is certain: "it obviously, deliberately, got that way."

Undefined意味着一个变量已经声明,但它没有被赋值,而Null可以赋给一个变量,表示“没有值”。(Null是赋值运算符)

2.Undefined是一个类型本身,而Null是一个对象。

3.Javascript本身可以将任何未赋值的变量初始化为undefined,但它永远不能将变量的值设置为null。这必须通过编程来完成。

摘自Nicholas C. Zakas的《面向对象的Javascript原理》

但是为什么对象类型是空的呢?(事实上,TC39(设计和维护JavaScript的委员会)已经承认这是一个错误。你可以推断null是一个空对象指针,使“object”成为一个逻辑返回值,但这仍然令人困惑。)

尼古拉斯·扎卡斯(2014-02-07)。面向对象JavaScript的原则(Kindle位置226-227)。没有淀粉机。Kindle版。

也就是说:

var game = null; //typeof(game) is "object"

game.score = 100;//null is not an object, what the heck!?
game instanceof Object; //false, so it's not an instance but it's type is object
//let's make this primitive variable an object;
game = {}; 
typeof(game);//it is an object
game instanceof Object; //true, yay!!!
game.score = 100;

未定义的例子:

var score; //at this point 'score' is undefined
typeof(score); //'undefined'
var score.player = "felix"; //'undefined' is not an object
score instanceof Object; //false, oh I already knew that.