为什么null在JavaScript中被认为是一个对象?
正在检查
if ( object == null )
Do something
和
if ( !object )
Do something
?
还有:
null和undefined之间的区别是什么?
为什么null在JavaScript中被认为是一个对象?
正在检查
if ( object == null )
Do something
和
if ( !object )
Do something
?
还有:
null和undefined之间的区别是什么?
当前回答
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
其他回答
摘自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.
理解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
}
为了补充“undefined和null之间的区别是什么”的答案,请参阅JavaScript权威指南第6版,本页第41页:
你可能认为undefined表示系统级的,意外的, 或者类似错误的无值和null表示程序级, 正常的,或预期的价值缺失。如果你需要分配一个 将这些值传递给变量或属性或将其中一个值传递给 一个函数,null几乎总是正确的选择。
思考“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."
typeof null; // object
typeof undefined; // undefined
null值表示有意不存在任何对象值。它是JavaScript的基本值之一,在布尔操作中被视为假值。
var x = null;
var y;
X被声明并定义为null
Y声明了,但没有定义。它声明时没有值,所以没有定义。
Z没有被声明,所以如果你试图使用Z,它也是未定义的。