javascript基礎資料型別 (Elementary Data Type)與實值型別參考型別說明

來源:互聯網
上載者:User

javascript基礎資料型別 (Elementary Data Type)與實值型別參考型別說明

 

一、基礎資料型別 (Elementary Data Type)

在javascript中申明變數使用的關鍵字都是var,這點與其他的程式設計語言不盡相同,但是javascript亦含有五種基本的資料類型(也可以說是單一資料型別),它們分別是:Undefined,Null,Boolean,Number和String。還含有一種複雜資料類型—Object。

(1)、undefined——未申明,或者變數的值即為undefined或者未初始化;

(2)、boolean ——如果這變數的值是布爾類型;
(3)、string ——值是字串類型;
(4)、number ——值是數字類型;
(5)、object ——對象或者值為null;
typeof這個關鍵字是一定要說的,因為javascript是鬆散類型的,在變數申明時並沒有使用與之類型相對應的關鍵字,如果在代碼中想要獲知某個變數的基本資料量,就可以使用typeof。這裡要注意的是typeof返回的是字串類型。

(5)、function ——函數。

執行個體驗證:

 

<script type=text/javascript>function test1(){var testMessage;alert(typeof testMessage);}function test2(){var testMessage = null;alert(typeof testMessage);}function test3(){var testMessage = hello;alert(typeof testMessage)}function test4(){var testMessage = 12;alert(typeof testMessage)}function test5(){var testMessage = true;alert(typeof testMessage)}function test6(){var testMessage = [];alert(typeof testMessage)}function test7(){var testMessage = [];alert(typeof testMessage)}function test8(){var testMessage = new Object();alert(typeof testMessage)}function test9(){alert(typeof test8)}</script>測試undefined測試null測試string測試number測試boolean測試[]測試{}測試Object測試function

1、Undefined
Undefined類型只有一個值,即undefined。當聲明的變數還未被初始化時,變數的預設值為undefined

 

function test1(){var testMessage;alert(typeof testMessage);}

2、Null

Null類型也只有一個值,即null。null用來表示尚未存在的對象,常用來表示函數企圖返回一個不存在的對象

 

 

function test2(){var testMessage = null;alert(typeof testMessage);}

 

3、string

字串,字串可以是引號中的任意文本。可以使用單引號或雙引號:

 

function test3(){var testMessage = hello;alert(typeof testMessage)}
4、number

 

可以是浮點數,整數

 

function test4(){var testMessage = 12;alert(typeof testMessage)}

 

5、boolean

布爾型,有兩個值 true or false.

function test5(){var testMessage = true;alert(typeof testMessage)}
6、obeject:

對象與數組,還有null也是。對象和數組裡都可以包含不同的類型,包括對象和數組。

 

function test6(){var testMessage = [];alert(typeof testMessage)}function test7(){var testMessage = [];alert(typeof testMessage)}function test8(){var testMessage = new Object();alert(typeof testMessage)}

 

 

 

7、function

函數類型

 

function test9(){alert(typeof test8)}

 

二、實值型別與參考型別

(1)實值型別:數值、布爾值、null、undefined

實值型別指的是儲存在棧記憶體中的簡單資料區段,按值訪問,操作的是他們實際儲存的值;

 

(2)參考型別:對象、數組、函數

 

參考型別指的是那些儲存在堆記憶體中的對象,意思是,變數中儲存的實際上只是一個指標,這個指標執行記憶體中的另一個位置,由該位置儲存對象;引用訪問,當查詢時,我們需要先從棧中讀取記憶體位址,然後再順藤摸瓜地找到儲存在堆記憶體中的值;

如:以下都是參考型別

var cars=   new Array;var person= new Object;

 

1、實值型別執行個體:

 

<script type=text/javascript>function fun1(){var a=1;var b=a;b=-1;alert(a=+a+ b=+b);}function fun2(){var a=new String(lin);var b=a;b = new String(bing);alert(a=+a+ b=+b);}function fun3(){var a=lin;var b=a;b = bing;alert(a=+a+ b=+b);}</script>測試實值型別測試實值型別測試實值型別

 

2、參考型別執行個體

 

<script type=text/javascript>function fun1(){var a=[1,2,3];var b=a;a[0]=1000;alert(a=+a+ b=+b);}function fun2(){var a = [1,2,3];var b = a;b = [11, 12, 13];//b指向了另一個記憶體位址,與a斷開關聯a[0] = 2;alert(a=+a+ b=+b);}function fun3(){ function ClassDemo(){ this.name = linbingwen; this.url = 我的部落格:http://blog.csdn.net/evankaka; } var objDemo = new ClassDemo(); var objDemo1 = objDemo; var objDemo2 = objDemo; objDemo1.url = 我的首頁:http://my.csdn.net/Evankaka; alert( objDemo1.url的值: + objDemo1.url + + objDemo2.url的值: + objDemo2.url);}</script>測試參考型別測試參考型別測試參考型別

 

注意:
undefined,null,Null 字元串,0都等於false,都可以通過!來取反。
 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.