JavaScript數(shù)據(jù)類型檢測(cè)實(shí)現(xiàn)方法詳解
一、typeof
- 優(yōu)點(diǎn):能快速判斷基本數(shù)據(jù)類型,除了
Null
; - 缺點(diǎn):不能判別
Object
、Array
、Null
,都返回object
;判別引用類型除函數(shù)顯示function
外,其他顯示為object
console.log(typeof 55); // number console.log(typeof true); // boolean console.log(typeof 'aa'); // string console.log(typeof undefined); // undefined console.log(typeof function(){}); // function console.log(typeof Symbol("foo")); // symbol console.log(typeof 553119869n); // bigint // 不能判別 console.log(typeof []); // object console.log(typeof {}); // object console.log(typeof null); // object
二、instanceof
MDN:
instanceof
運(yùn)算符 用于檢測(cè)構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個(gè)實(shí)例對(duì)象的原型鏈上。
理解:判斷在其原型鏈中能否找到該類型的原型。
語法:
object instanceof constructor
function D(){} var o = new D(); o instanceof D; // true o instanceof Object; // true
- 優(yōu)點(diǎn):能區(qū)分
Array
、Object
和Function
,適用于判斷自定義的類實(shí)例對(duì)象 - 缺點(diǎn):不能判斷
Number
,Boolean
,String
基本數(shù)據(jù)類型
console.log(55 instanceof Number); // false console.log(true instanceof Boolean); // false console.log('aa' instanceof String); // false console.log([] instanceof Array); // true console.log(function(){} instanceof Function); // true console.log({} instanceof Object); // true
String 對(duì)象和 Date 對(duì)象都屬于 Object 類型 和 一些特殊情況:
var simpleStr = "a simple string"; var objStr = new String(); var newStr = new String("String created with constructor"); var aDate = new Date(); var myNonObj = Object.create(null); simpleStr instanceof String; // false,非對(duì)象實(shí)例,因此返回 false objStr instanceof String; // true newStr instanceof String; // true objStr instanceof Object; // true myNonObj instanceof Object; // false,一種創(chuàng)建非 Object 實(shí)例的對(duì)象的方法 aDate instanceof Date; // true aDate instanceof Object; // true
三、Object.prototype.toString.call()
- 優(yōu)點(diǎn):精準(zhǔn)判斷數(shù)據(jù)類型,所有原始數(shù)據(jù)類型都是能判斷;
- 缺點(diǎn):寫法繁瑣,最好進(jìn)行封裝后使用
var toString = Object.prototype.toString; console.log(toString.call(55)); // [object Number] console.log(toString.call(true)); // [object Boolean] console.log(toString.call('aa')); // [object String] console.log(toString.call([])); // [object Array] console.log(toString.call(function(){})); // [object Function] console.log(toString.call({})); // [object Object] console.log(toString.call(undefined)); // [object Undefined] console.log(toString.call(null)); // [object Null] console.log(toString.call(Math)); // [object Math] console.log(toString.call(Set)); // [object Function] Set 構(gòu)造函數(shù) console.log(toString.call(Array)); // [object Function] Array 構(gòu)造函數(shù) console.log(toString.call(Map)); // [object Function] console.log(toString.call(Date)); // [object Function] console.log(toString.call(new Set())); // [object Set] console.log(toString.call(new Array())); // [object Array] console.log(toString.call(new Map())); // [object Map] console.log(toString.call(new Date())); // [object Date] function D(){} console.log(toString.call(D)); // [object Function] console.log(toString.call(new D())); // [object Object]
面試問題
如何判斷變量是否為數(shù)組?
let arr = [] console.log(Array.isArray(arr)); // true arr.__proto__ === Array.prototype; // true arr instanceof Array; // true Object.prototype.toString.call(arr);// [object Array]
判斷是否是 Promise 對(duì)象
function isPromise(val) { return ( typeof val.then === 'function' && typeof val.catch === 'function' ) } let p = new Promise((resolve, reject) => {}); console.log(isPromise(p)); // true
到此這篇關(guān)于JavaScript數(shù)據(jù)類型檢測(cè)實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)JS數(shù)據(jù)類型檢測(cè)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
KnockoutJS 3.X API 第四章之事件event綁定
event綁定即為事件綁定,即當(dāng)觸發(fā)相關(guān)DOM事件的時(shí)候回調(diào)函數(shù),這篇文章主要介紹了KnockoutJS 3.X API 第四章之事件event綁定的相關(guān)知識(shí),感興趣的朋友一起看看吧2016-10-10JavaScript實(shí)現(xiàn)模塊拖拽功能的代碼示例
這篇文章將給大家詳細(xì)介紹一下JavaScript實(shí)現(xiàn)模塊的拖拽功能的代碼示例,文中有詳細(xì)的實(shí)現(xiàn)步驟,需要的朋友可以參考下2023-07-07在JavaScript中使用Promise.allSettled()的方法
Promise.allSettled()是一個(gè)游戲規(guī)則改變者,讓您等待所有承諾得到解決(解決或拒絕),然后根據(jù)結(jié)果采取行動(dòng),這篇文章主要介紹了如何在JavaScript中使用Promise.allSettled(),需要的朋友可以參考下2023-07-07合并多個(gè)ArrayBuffer場(chǎng)景及方法示例
這篇文章主要為大家介紹了合并多個(gè)ArrayBuffer方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11JS使用Promise控制請(qǐng)求并發(fā)數(shù)
現(xiàn)在面試過程當(dāng)中 ,手寫題必然是少不了的,其中碰到比較多的無非就是當(dāng)屬 請(qǐng)求并發(fā)控制了,所以本文為大家整理了JS使用Promise控制請(qǐng)求并發(fā)數(shù)的示例代碼,希望對(duì)大家有所幫助2023-05-05