js中null,undefined和空的區別

// undefined代表爲未定義
// "undefined"表示一個字符串
// typeof(exp)爲一個字符串,表示類型,可以用typeof(typeof(exp))證明
var exp = "undefined";
if (exp == undefined) //不相等  一個爲undefined類型 一個爲string類型
{
   alert(typeof(typeof(exp)));
}
//(typeof(exp)爲一個sting類型,值爲"undefined"
//typeof(exp)=="undefined"是相等的  typeof(exp)==undefined是不相等的
//null==undefined是相等的

 

null表示"沒有對象",即該處不應該有值。典型用法是:

(1) 作爲函數的參數,表示該函數的參數不是對象。

(2) 作爲對象原型鏈的終點。

Object.getPrototypeOf(Object.prototype)

// null

undefined表示"缺少值",就是此處應該有一個值,但是還沒有定義。典型用法是:

(1)變量被聲明瞭,但沒有賦值時,就等於undefined。

(2) 調用函數時,應該提供的參數沒有提供,該參數等於undefined。

(3)對象沒有賦值的屬性,該屬性的值爲undefined。

(4)調用對象沒有的屬性,返回值爲undefined。

(5)函數沒有返回值時,默認返回undefined。

var i;
i // undefined

function f(x){console.log(x)}
f() // undefined

var o = new Object();
o.p // undefined

var x = f();
x // undefined

詳情:https://www.cnblogs.com/yangzhx/p/4019073.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章