JavaScript數(shù)據(jù)類型檢測實(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)算符 用于檢測構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個(gè)實(shí)例對象的原型鏈上。
理解:判斷在其原型鏈中能否找到該類型的原型。
語法:
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í)例對象 - 缺點(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 對象和 Date 對象都屬于 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,非對象實(shí)例,因此返回 false
objStr instanceof String; // true
newStr instanceof String; // true
objStr instanceof Object; // true
myNonObj instanceof Object; // false,一種創(chuàng)建非 Object 實(shí)例的對象的方法
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 對象
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ù)類型檢測實(shí)現(xiàn)方法詳解的文章就介紹到這了,更多相關(guān)JS數(shù)據(jù)類型檢測內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
小程序如何寫動態(tài)標(biāo)簽的實(shí)現(xiàn)方法
這篇文章主要介紹了小程序如何寫動態(tài)標(biāo)簽的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
JavaScript遍歷實(shí)現(xiàn)DFS算法和BFS算法
DFS(Depth?first?search)稱作「深度優(yōu)先遍歷」,BFS(Breadth?first?search)稱作「廣度優(yōu)先遍歷」。本文將通過JavaScript遍歷實(shí)現(xiàn)這兩種算法,需要的可以參考一下2023-01-01
基于JavaScript實(shí)現(xiàn)報(bào)警器提示音效果
這篇文章給大家分享分享一段代碼基于JavaScript實(shí)現(xiàn)報(bào)警器提示音效果,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-10-10
微信小程序使用webview頁面轉(zhuǎn)pdf文件代碼示例
工作需求,將webview的內(nèi)容導(dǎo)出到pdf輸出,下面這篇文章主要給大家介紹了關(guān)于微信小程序使用webview頁面轉(zhuǎn)pdf文件的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-09-09
JavaScript定時(shí)器setTimeout()和setInterval()詳解
這篇文章主要為大家詳細(xì)介紹了JavaScript定時(shí)器setTimeout()和setInterval()的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
在knockoutjs 上自己實(shí)現(xiàn)的flux(實(shí)例講解)
下面小編就為大家分享一篇在knockoutjs 上自己實(shí)現(xiàn)的flux方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
js+html+css實(shí)現(xiàn)簡單日歷效果
這篇文章主要為大家詳細(xì)介紹了js+html+css實(shí)現(xiàn)簡單日歷效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06

