js中的null 和undefined

來源:互聯網
上載者:User

標籤:字元   number   lock   表示   html   boolean   function   undefined   原型鏈   

參考連結:http://blog.csdn.net/qq_26676207/article/details/53100912

http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

http://www.tuicool.com/articles/b6vqmy

關於什麼時候會出現null什麼時候出現undefined:

這麼答:

變數定義未賦值時是undefined,或者是typeof 一個定義為未賦值的變數或沒有的定義的變數;

null,表示一個Null 物件,是原型鏈的鐘點;當釋放一個空間時,把變數賦值null;讓記憶體回收機制去回收它,typeof null是object;

typeof 返回的是字串,有六種可能:"number"、"string"、"boolean"、"object"、"function"、"undefined";

未定義的值和定義未賦值的為undefined,null是一種特殊的object,NaN是一種特殊的number。

var a1;
var a2 = true;
var a3 = 1;
var a4 = "Hello";
var a5 = new Object();
var a6 = null;
var a7 = NaN;
var a8 = undefined;
alert(typeof a); //顯示"undefined"
alert(typeof a1); //顯示"undefined"
alert(typeof a2); //顯示"boolean"
alert(typeof a3); //顯示"number"
alert(typeof a4); //顯示"string"
alert(typeof a5); //顯示"object"
alert(typeof a6); //顯示"object"
alert(typeof a7); //顯示"number"
alert(typeof a8); //顯示"undefined"

== 運算子認為 undefined 值等價於 null。

null表示的是一個明確的已經知道的值,是一個Null 物件

 

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

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

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

Object.getPrototypeOf(Object.prototype)// null




從邏輯角度上來講,null值表示一個null 指標對象

js中的null 和undefined

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.