js 數(shù)據(jù)類型判斷的方法
typeof
一般用于判斷基本數(shù)據(jù)類型,用于判斷引用數(shù)據(jù)類型和null時會發(fā)生意外的錯誤
typeof 1 // number typeof '1' // string typeof true // boolean typeof Symbol('1') // symbol typeof undefined // undefined typeof function(){} // function typeof { a: 1 } // object typeof [1, 2, 3] // object 這里會判斷異常,建議使用Array.isArray區(qū)分數(shù)組和對象 //以下也會判斷異常 typeof new Boolean(true) === 'object'; typeof new Number(1) === 'object'; typeof new String('abc') === 'object'; //最后來看null typeof null // object
來看下typeof的原理:不同的對象在底層都表示為二進制,在js里二進制前三位都為0的會 被判斷為object類型,null的二進制表示全0(對應(yīng)機器碼的null指針,一般為全0),所以會被判斷成object類型。
instanceof
它的主要作用是用來判斷一個實例是否屬于某種類型,用于判斷對象很合適
語法:object instanceof constructor object 某個實例對象 constructor 某個構(gòu)造函數(shù) 'abc' instanceof String //false 檢查原型鏈會返回undefined new String('abc') instanceof String //true new Boolean(true) instanceof Boolean // true new Number(1) instanceof Number // true 順便做一下簡單實現(xiàn) function new_instance_of(leftVaule, rightVaule) { let leftProto = leftVaule.__proto__; // 取左表達式的__proto__值 let rightPrototype = rightVaule.prototype; // 取右表達式的 prototype 值 while (true) { if (leftProto === null) { return false; } if (rightPrototype === rightProto) { return true; } leftProto = leftProto.__proto__ } }
constructor
根據(jù)數(shù)據(jù)類型的構(gòu)造函數(shù)返回類型,但是由于null和undefined沒有構(gòu)造函數(shù)故無法判斷
''.constructor == String //true new Number(1).constructor == Number //true new Function().constructor == Function //true true.constructor == Boolean //true new Date().constructor == Date //true
Object.prototype.toString.call()
可以通過 toString() 來獲取每個對象的類型。為了每個對象都能通過Object.prototype.toString() 來檢測,需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來調(diào)用,傳遞要檢查的對象作為第一個參數(shù)。
var toString = Object.prototype.toString; toString.call(new Date); // [object Date] toString.call(new String); // [object String] toString.call(Math); // [object Math] toString.call(undefined); // [object Undefined] toString.call(null); // [object Null]
lodash.getTag和lodash.baseGetTag
baseGetTag使用Object.prototype.toString.call和Symbol.toStringTag來判斷屬性的類型Symbol.toStringTag只適合做特定的類型判斷
//lodash.baseGetTag部分重要源碼 //如果值是undefined和null返回對應(yīng)tag if (value == null) { return value === undefined ? '[object Undefined]' : '[object Null]' } // 如果不支持Symbol或者value值上面沒有Symbol.toStringTag屬性, //直接返回Object.prototype.toString調(diào)用后的值 if (!(symToStringTag && symToStringTag in Object(value))) { return toString.call(value) }
以上就是js 數(shù)據(jù)類型判斷的方法的詳細內(nèi)容,更多關(guān)于js 數(shù)據(jù)類型判斷的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
用Javascript實現(xiàn)Windows任務(wù)管理器的代碼
在Windows系統(tǒng)上,自從98系統(tǒng)以來就提供了腳本宿主(Windows Scripting Host 簡稱WSH)的功能,WSH可以加載并運行JS和VBS腳本,并支持調(diào)用系統(tǒng)的COM組件,在COM組件的支持下腳本可以輕松實現(xiàn)非常強大的功能2012-03-03JS getAttribute和setAttribute(取得和設(shè)置屬性)的使用介紹
本篇文章是對JS中的getAttribute和setAttribute(取得和設(shè)置屬性)的使用進行了詳細的分析介紹,需要的朋友可以參考下2013-07-07JS前端面試必備——基本排序算法原理與實現(xiàn)方法詳解【插入/選擇/歸并/冒泡/快速排序】
這篇文章主要介紹了JS前端面試基本排序算法原理與實現(xiàn)方法,結(jié)合實例形式詳細分析了JS常見的基本排序算法相關(guān)原理、實現(xiàn)方法、時間復(fù)雜度及操作注意事項,需要的朋友可以參考下2020-02-02