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

正在检查

if ( object == null )
      Do something

if ( !object )
      Do something

?

还有:

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

其他回答

思考“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."

(name is undefined)

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

name = null;

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

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

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

name = false;

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

name = '';

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


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

使用null来定义没有值的东西,使用undefined当你期望一些东西可能根本没有定义。

例如,如果一个变量没有值,将其赋值为null。

var weDontHaveAValue = null;

如果你期望某些东西可能根本没有定义,例如一个可选的选项参数,使用undefined。

if (typeof args.optionalParam !== 'undefined') { }

Null和undefined对于值相等都是false (Null ==undefined):它们都崩溃为布尔值false。它们不是同一个对象(null!==undefined)。

Undefined是全局对象(浏览器中的“window”)的一个属性,但它是一个基本类型,而不是对象本身。它是未初始化的变量和函数的默认值,没有返回语句。

null是Object的一个实例。null用于返回集合对象以指示空结果的DOM方法,该方法提供false值而不指示错误。

Null不是一个对象,它是一个基本值。例如,不能向其添加属性。有时人们错误地认为它是一个对象,因为typeof null返回“object”。但这实际上是一个错误(甚至可能在ECMAScript 6中被修复)。

null和undefined的区别如下:

undefined: used by JavaScript and means “no value”. Uninitialized variables, missing parameters and unknown variables have that value. > var noValueYet; > console.log(noValueYet); undefined > function foo(x) { console.log(x) } > foo() undefined > var obj = {}; > console.log(obj.unknownProperty) undefined Accessing unknown variables, however, produces an exception: > unknownVariable ReferenceError: unknownVariable is not defined null: used by programmers to indicate “no value”, e.g. as a parameter to a function.

检查变量:

console.log(typeof unknownVariable === "undefined"); // true

var foo;
console.log(typeof foo === "undefined"); // true
console.log(foo === undefined); // true

var bar = null;
console.log(bar === null); // true

作为一般规则,在JavaScript中应该总是使用===,而永远不要使用==(==执行各种可能产生意外结果的转换)。检查x == null是一个边缘情况,因为它适用于null和undefined:

> null == null
true
> undefined == null
true

检查变量是否有值的一种常见方法是将其转换为布尔值,然后看它是否为真。该转换由if语句和布尔运算符!(“不”)。

function foo(param) {
    if (param) {
        // ...
    }
}
function foo(param) {
    if (! param) param = "abc";
}
function foo(param) {
    // || returns first operand that can't be converted to false
    param = param || "abc";
}

这种方法的缺点:以下所有值的计算结果都是false,所以你必须小心(例如,上面的检查不能区分undefined和0)。

定义,零 布尔值:假 数字:+0,-0,NaN 弦:“”

你可以通过使用boolean作为函数来测试到boolean的转换(通常它是一个构造函数,用于new):

> Boolean(null)
false
> Boolean("")
false
> Boolean(3-3)
false
> Boolean({})
true
> Boolean([])
true