Javascript 資料類型

來源:互聯網
上載者:User

標籤:style   object   span   text   obj   on()   const   判斷   繼承   

一、js的資料類型

   1、基本類型:字串類型(string),數字類型(number),布爾類型(boolean)

   2、複雜類型:數群組類型(array),物件類型(object),函數類型(function),正則類型(regexp)

   3、空類型:undefine  和 null

二、javascript中類型檢測方法有很多,簡要介紹一下兩種:

  • typeof
  • instanceof

1、typeof

   typeof  100                "number"   typeof   nan               "number "   typeof  false              " boolean"   typeof   function         "function "   typeof   (undefined)   "undefined "   typeof   null                "object "   typeof   [1,2]               "object "             

 特殊的是typeof null返回“object”。

typeof對基本類型和函數對象很方便,但是其他類型就沒辦法了。

例如想判斷一個對象是不是數組?用typeof返回的是“object”。所以判斷對象的類型常用instanceof。

2、instanceof  運算子用來檢測 constructor.prototype 是否存在於參數 object 的原型鏈上。instanceof只能用來判斷對象和函數,不能用來判斷字串和數字等

obj instanceof Object 檢測Object.prototype是否存在於參數obj的原型鏈上。

function Person(){};var p =new Person();console.log(p instanceof Person);   //true

繼承中判斷執行個體是否屬於它的父類

Student和Person都在s的原型鏈中:

function Person(){};function Student(){};var p =new Person();Student.prototype=p;//繼承原型var s=new Student();console.log(s instanceof Student);//trueconsole.log(s instanceof Person);//true
var oStringObject = new String("hello world"); console.log(oStringObject instanceof String);   // 輸出 "true"

 

 

 

Javascript 資料類型

聯繫我們

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