为什么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是一个对象。它的类型是object。Null是一个特殊值,表示“没有值”。Undefined不是一个对象,它的类型是Undefined。
你可以声明一个变量,将其设置为null,除了你会看到“null”和“undefined”打印出来之外,行为是相同的。你甚至可以将一个未定义的变量与null进行比较,反之亦然,条件将为真:
undefined == null
null == undefined
更多细节请参考JavaScript null和undefined之间的差异。
还有你的新编辑,是的
if (object == null) does mean the same if(!object)
当测试object是否为false时,它们都只满足测试是否为false时的条件,而不满足测试是否为true时的条件
检查这里:Javascript抓住你了
其他回答
Undefined意味着一个变量已经声明,但它没有被赋值,而Null可以赋给一个变量,表示“没有值”。(Null是赋值运算符)
2.Undefined是一个类型本身,而Null是一个对象。
3.Javascript本身可以将任何未赋值的变量初始化为undefined,但它永远不能将变量的值设置为null。这必须通过编程来完成。
看看这个:
<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
Null和undefined对于值相等都是false (Null ==undefined):它们都崩溃为布尔值false。它们不是同一个对象(null!==undefined)。
Undefined是全局对象(浏览器中的“window”)的一个属性,但它是一个基本类型,而不是对象本身。它是未初始化的变量和函数的默认值,没有返回语句。
null是Object的一个实例。null用于返回集合对象以指示空结果的DOM方法,该方法提供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."
理解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
}