深入瞭解JavaScript的核心ECMAScript

來源:互聯網
上載者:User

為了更好的深入瞭解JavaScript是如何運作的,必須先瞭解JavaScript的核心ECMAScript。ECMAScript的變數可以分為原始值和引用值

原始類型:Undefined,Number,String,Boolean,Null.

判斷變數的類型使用typeof操作符 可能返回"undefined","boolean","string","number","object","function"

以下是引用片段:
1 <script type="text/javascript">
2 function functionType()
3 {}
4 window.onload = function()
5 {
6 var stringType = "abc";
7 var numberType = 123;
8 var boolType = true;
9 window.alert(typeof stringType); //輸出"string"
10 window.alert(typeof numberType); //輸出"number"
11 window.alert(typeof boolType); //輸出"boolean"
12 window.alert(typeof functionType); //輸出"function"
13 window.alert(typeof undefinedType); //輸出"undefined"
14 window.alert(typeof null); //輸出"object"
15 window.alert(typeof Math); //輸出"object" Math是內建對象
16 }
17 </script>

比較特別的地方 typeof運算子對於null值會返回"object"。這實際上是JavaScript最初實現中的一個錯誤,然後被ECMAScript沿用了。現在,null被認為是對象的預留位置,從而解釋了這一矛盾,但從技術上來說,它仍然是原始值。

Undefined類型

Undefined類型只有一個值 就是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.