標籤:style object span text obj on() const 判斷 繼承
一、js的資料類型
1、基本類型:字串類型(string),數字類型(number),布爾類型(boolean)
2、複雜類型:數群組類型(array),物件類型(object),函數類型(function),正則類型(regexp)
3、空類型:undefine 和 null
二、javascript中類型檢測方法有很多,簡要介紹一下兩種:
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 資料類型