JavaScript怎么判斷數(shù)據(jù)類型?本篇文章給大家分享JS 判斷數(shù)據(jù)類型的 8 種方式,有效幫助工作和面試,面試官看了微微一笑。
注意:null、 NaN、 document.all的判斷
console.log(typeof null); // objectconsole.log(typeof NaN); // numberconsole.log(typeof document.all); // undefined
constuctor指向創(chuàng)建該實(shí)例對象的構(gòu)造函數(shù)注意 null和 undefined沒有 constructor,以及 constructor可以被改寫
(資料圖片僅供參考)
String.prototype.constructor = function fn() { return {};};console.log("云牧".constructor); // [Function: fn]obj instanceof Type功能:判斷 obj是不是 Type類的實(shí)例,只可用來判斷引用數(shù)據(jù)實(shí)現(xiàn)思路: Type的原型對象是否是 obj的原型鏈上的某個對象注意:右操作數(shù)必須是函數(shù)或者 class手寫 instanceof:
function myInstanceof(Fn, obj) { // 獲取該函數(shù)顯示原型 const prototype = Fn.prototype; // 獲取obj的隱式原型 let proto = obj.__proto__; // 遍歷原型鏈 while (proto) { // 檢測原型是否相等 if (proto === prototype) { return true; } // 如果不等于則繼續(xù)往深處查找 proto = proto.__proto__; } return false;}instanceofconsole.log(Object.isPrototypeOf({})); // falseconsole.log(Object.prototype.isPrototypeOf({})); // true 期望左操作數(shù)是一個原型,{} 原型鏈能找到 Object.prototypefunction typeOf(data) { return Object.prototype.toString.call(data).slice(8, -1);}// 測試console.log(typeOf(1)); // Numberconsole.log(typeOf("1")); // Stringconsole.log(typeOf(true)); // Booleanconsole.log(typeOf(null)); // Nullconsole.log(typeOf(undefined)); // Undefinedconsole.log(typeOf(Symbol(1))); // Symbolconsole.log(typeOf({})); // Objectconsole.log(typeOf([])); // Arrayconsole.log(typeOf(function () {})); // Functionconsole.log(typeOf(new Date())); // Dateconsole.log(typeOf(new RegExp())); // RegExpkindof與 p-is-promisep-is-promise:
const isObject = value => value !== null && (typeof value === "object" || typeof value === "function");export default function isPromise(value) { return ( value instanceof Promise || (isObject(value) && typeof value.then === "function" && typeof value.catch === "function") );}kindof:
function kindof(obj) { var type; if (obj === undefined) return "undefined"; if (obj === null) return "null"; switch ((type = typeof obj)) { case "object": switch (Object.prototype.toString.call(obj)) { case "[object RegExp]": return "regexp"; case "[object Date]": return "date"; case "[object Array]": return "array"; } default: return type; }}Object.prototype.toString會讀取該值適用場景:需自定義類型注意事項(xiàng):兼容性class MyArray { get [Symbol.toStringTag]() { return "MyArray"; }}const arr = new MyArray();console.log(Object.prototype.toString.call(arr)); // [object MyArray]undefined、 window、 document、 null等underscore.js:
| 方法 | 基礎(chǔ)數(shù)據(jù)類型 | 引用類型 | 注意事項(xiàng) |
|---|---|---|---|
| typeof | √ | × | NaN、object、document.all |
| constructor | √ 部分 | √ | 可以被改寫 |
| instanceof | × | √ | 多窗口,右邊構(gòu)造函數(shù)或者class |
| isPrototypeof | × | √ | 小心 null 和 undefined |
| toString | √ | √ | 小心內(nèi)置原型 |
| 鴨子類型 | - | √ | 不得已兼容 |
| Symbol.toString Tag | × | √ | 識別自定義對象 |
| 等比較 | √ | √ | 特殊對象 |
typeof后是數(shù)字
自己不等于自己
delete不能被刪除
NaN,就返回 true,反之返回 falseconsole.log(isNaN(NaN)); // trueconsole.log(isNaN({})); // trueNaNconsole.log(Number.isNaN(NaN)); // trueconsole.log(Number.isNaN({})); // false其他判斷是否 NaN的方法
function isNaNVal(val) { return Object.is(val, NaN);}function isNaNVal(val) { return val !== val;}function isNaNVal(val) { return typeof val === "number" && isNaN(val);}// 綜合墊片if (!("isNaN" in Number)) { Number.isNaN = function (val) { return typeof val === "number" && isNaN(val); };}indexOf不可查找 NaN,includes則可以const arr = [NaN];console.log(arr.indexOf(NaN)); // -1console.log(arr.includes(NaN)); // true
【推薦學(xué)習(xí):javascript高級教程】
以上就是JavaScript怎么判斷數(shù)據(jù)類型?8 種方式分享的詳細(xì)內(nèi)容,更多請關(guān)注php中文網(wǎng)其它相關(guān)文章!
關(guān)鍵詞: JavaScript