动态

详情 返回 返回

深度解析javaScript常見數據類型檢查校驗 - 动态 详情

前言

在JavaScript中,數據類型分為兩大類,一種是基礎數據類型,另一種則是複雜數據類型,又叫引用數據類型

  • 基礎數據類型:數字Number 字符串String 布爾Boolean Null Undefined Symbols BigInt
  • 引用數據類型:日期Dete,對象Object,數組Array,方法Function, 正則regex,帶鍵的集合:Maps, Sets, WeakMaps, WeakSets

基礎數據類型和引用數據類型的區別,在之前深拷貝的文章中提到過,這裏不做詳細贅述。

傳送門:javaScript中深拷貝和淺拷貝簡單梳理

常見的幾種數據校驗方式

接下來會針對下面幾種數據類型,進行校驗

// 基本數據類型
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);

// 複雜-引用數據類型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map();

typeof操作符

typeof操作符,會返回一個字符串,表示未經計算的操作數的類型

/**
 * typeof 操作符
 *
 * 返回一個字符串,表示未經計算的操作數的類型。
 *
 * */
console.log(typeof str); //  string
console.log(typeof num); //  number
console.log(typeof boo); //  boolean
console.log(typeof undef); //  undefined
console.log(typeof testNull); //  object
console.log(typeof symb); //  symbol
console.log(typeof bigInt); //  bigint
console.log(typeof Object(bigInt)); // object
console.log(typeof arr); //  object
console.log(typeof func); //  function
console.log(typeof obj); //  object
console.log(typeof date1); //  object
console.log(typeof setObj1); //  object
console.log(typeof setObj2); //  object
console.log(typeof mapObj); //  object
小結

使用typeof操作符的時候,我們可以看到一些較為特殊的情況:

  • null,數組array,set,map 返回的是對象object

instanceof

instanceof用於檢測構造函數的 prototype 屬性是否出現在某個實例對象的原型鏈上。

/**
 *
 * instanceof
 *
 * 用於檢測構造函數的 prototype 屬性是否出現在某個實例對象的原型鏈上。
 *
 * */

console.log(str instanceof String); // false

console.log(new String("abc") instanceof String); // true

console.log(num instanceof Number); // false

console.log(new Number(123) instanceof Number); // true

console.log(boo instanceof Boolean); // false

console.log(new Boolean(true) instanceof Boolean); // false

console.log(undef instanceof undefined);
// Uncaught TypeError: Right-hand side of 'instanceof' is not an object

console.log(testNull instanceof null);
// Uncaught TypeError: Right-hand side of 'instanceof' is not an object

console.log(symb instanceof Symbol); // false 
// Symbol不是構造函數,沒有new操作符

console.log(bigInt instanceof BigInt); // false

console.log(Object(BigInt("22")) instanceof Object); // true

console.log(Object(BigInt("22")) instanceof BigInt); // true

console.log(arr instanceof Array); // true

console.log(arr instanceof Object); // true

console.log(func instanceof Function); // true

console.log(func instanceof Object); // true

console.log(obj instanceof Object); // true

console.log(obj instanceof Function); // false

console.log(null instanceof Object); // false

console.log(date1 instanceof Object); // true

console.log(setObj1 instanceof Object); //  true

console.log(setObj2 instanceof Object); //  true

console.log(mapObj instanceof Object); //  true

console.log(setObj1 instanceof Array); //  false

console.log(setObj2 instanceof Array); //  false

console.log(mapObj instanceof Array); //  false

constructor


/**
 * constructor
 *
 * 返回創建實例對象的 構造函數的引用。
 *
 * 注意,此屬性的值是對函數本身的引用,而不是一個包含函數名稱的字符串
 *
 * 構造函數.prototype.constructor()
 *
 * */
 
// 基本數據類型
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);

// 複雜-引用數據類型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
function constructorFn() {
  this.name = "11";
}
let con1 = new constructorFn();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map(); 

console.log(str.constructor); // String

console.log(num.constructor); // Number

console.log(boo.constructor); // Boolean

// console.log(testUndefined.constructor); // Cannot read property 'constructor' of undefined

// console.log(testNull.constructor); // Cannot read property 'constructor' of null
console.log(symb.constructor); // Symbol

console.log(bigInt.constructor); // BigInt

console.log(arr.constructor); // Array

console.log(func.constructor); // Function

console.log(obj.constructor); // Object

console.log(date1.constructor); // Date

console.log(constructorFn.constructor); // Function

console.log(con1.constructor); // constructorFn

console.log(setObj1.constructor); // Set

console.log(setObj2.constructor); // Set

console.log(mapObj.constructor); // Map


/**
 *
 * 構造函數校驗
 *
 * */

console.log(Function.constructor); // Function
console.log(Object.constructor); // Function
console.log(Array.constructor); // Function
console.log(Date.constructor); // Function

Object.prototype.toString.call && Object.prototype.toString.apply

Object.prototype.toString()

在使用Object.prototype.toString.call或者Object.prototype.toString.apply檢查數據類型之前,我們先了解一下Object.prototype.toString和JavaScript中的構造函數Function的原型方法apply和call:

/**
 * 返回一個表示該對象的字符串
 *
 * Object.prototype.toString()
 *
 * 每個對象都有一個 toString() 方法,當該對象被表示為一個文本值時,或者一個對象以預期的字符串方式引用時自動調用。
 * 默認情況下,toString() 方法被每個 Object 對象繼承。
 *
 * 如果此方法在自定義對象中未被覆蓋,toString() 返回 "[object type]",其中 type 是對象的類型。以下代碼説明了這一點:
 *
 * */

let isObj = { name: "zhangsan" };
let isBoolean = true;
let isNumber = new Number(123);
let isString = "abc";
let isFun = new Function();
console.log(isObj.toString()); // [object Object]
console.log(isBoolean.toString()); // true
console.log(isNumber.toString()); // 123
console.log(isString.toString()); // abc
console.log(new Date().toString()); // Thu Apr 28 2022 16:37:19 GMT+0800 (中國標準時間)
console.log(isFun.toString()); // function anonymous() {}
call && apply
/**
 *
 * call() 使用一個指定的 this 值和單獨給出的一個或多個參數來調用一個函數,function.call(thisArg, arg1, arg2, ...)
 *
 * apply() 使用一個指定的 this 值和單獨給出的一個或多個參數來調用一個函數,unc.apply(thisArg, [argsArray])
 *
 * */

// call基本使用;
function a() {
    console.log(this);
}

function b() {
    console.log(this);
}
a.call(b); //        function b() {}
b.call(a); //        function a() {}
  • call和apply最簡單的例子表明了,改變了當前方法的this指向
  • 同時這兩個方法的區別在於傳參的方式

Object.prototype.toString結合Function.prototype.call && apply


/**
 *
 * 使用 toString() 檢測對象類型可以通過 toString() 來獲取每個對象的類型。
 * 為了每個對象都能通過 Object.prototype.toString() 來檢測,
 * 需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來調用,傳遞要檢查的對象作為第一個參數,稱為 thisArg。
 *
 * 那麼 Object.prototype.toString 相當於 原生構造函數的實例化對象isNumber,傳參數給Object.prototype.toString執行
 * 實際上相當於 toString.call(new ***);
 *
 * */
 
let str = "abc";
let num = 123;
let boo = true;
let undef = undefined;
let testNull = null;
let symb = Symbol("user");
let bigInt = BigInt(9007199254740999);

// 複雜-引用數據類型
let arr = [1, 2, 3, 4];
let func = function () {};
let obj = {};
let date1 = new Date();
function testFun() {}
let newTest = new testFun();
let newFun = new Function();
let setObj1 = new Set();
let setObj2 = new Set([1, 2, 3]);
let mapObj = new Map();

console.log(Object.prototype.toString.apply(new String("sss"))); // [object String]

console.log(Object.prototype.toString.apply(str)); // [object String]

console.log(Object.prototype.toString.call(num)); // [object Number]

console.log(Object.prototype.toString.call(boo)); // [object Boolean]

console.log(Object.prototype.toString.call(undef)); // [object Undefined]

console.log(Object.prototype.toString.call(testNull)); // [object Null]

console.log(Object.prototype.toString.call(symb)); // [object Symbol]

console.log(Object.prototype.toString.call(Object(bigInt))); // [object BigInt]

console.log(Object.prototype.toString.call(bigInt)); // [object BigInt]

console.log(Object.prototype.toString.apply(arr)); // [object Array]

console.log(Object.prototype.toString.call(func)); // [object Function]

console.log(Object.prototype.toString.call(obj)); // [object Object]

console.log(Object.prototype.toString.call(date1)); // [object Date]

console.log(Object.prototype.toString.call(testFun)); // [object Function]

console.log(Object.prototype.toString.call(newTest)); // [object Object]

console.log(Object.prototype.toString.call(newFun)); // [object Function]

console.log(Object.prototype.toString.call(setObj1)); // [object Set]

console.log(Object.prototype.toString.call(setObj2)); // [object Set]

console.log(Object.prototype.toString.call(mapObj)); // [object Map]

其他校驗數據類型的方法:

判斷是否是數組:
console.log(Array.isArray([1, 2])); // true
判斷一個對象是否是空對象
// 判斷空對象
function isEmptyObj(obj) {
  for (name in obj) {
    console.log(name);
    return false; // 不是空對象
  }
  return true;
}
console.log(isEmptyObj({}));  // true

總結

  • 不管是typeof操作符,還是其他的操作方法,都有各自的缺陷
  • 在日常的開發過程中,我們需要知道當前操作的是對象,還是構造函數生成的對象或者方法,才能針對當前需要判斷的數據類型,採用最適合的方法
  • Object.prototype.toString.call或者Object.prototype.toString.apply應該是最完善的方法,在我們不確定是否是引用類型或者基本數據類型的時候,建議作為首選
  • 在瞭解這些判斷數據類型的方式之前或者説存有疑問:為什麼array數組在使用instanceof和typeof 校驗Object的時候都成立,這時候需要去了解一下引用數據類型的具體內容
  • 以上判斷數據類型的方法,可以在項目開發過程中,可以寫入到utils公共方法當中,作為開發中進行使用。
  • 更多用法等待補充。

源碼地址

  • 碼雲 https://gitee.com/lewyon/vue-note
  • githup https://github.com/akari16/vue-note

文章個人博客地址:深度解析javaScript常見數據類型檢查校驗

歡迎關注公眾號:程序員布歐,不定期更新一些前端入門文章

創作不易,轉載請註明出處和作者。

user avatar front_yue 头像 inslog 头像 zzd41 头像 guixiangyyds 头像 zhulongxu 头像 youyoufei 头像 joe235 头像 esunr 头像 jinl9s27 头像 happy2332333 头像 haoqidewukong 头像 haikuotiankong_mac 头像
点赞 66 用户, 点赞了这篇动态!
点赞

Add a new 评论

Some HTML is okay.