JavaScript中如何判斷數據類型?

JavaScript判斷數據類型可通過typeof、instanceof、constructorObject.prototype.toString.call()實現。1. typeof適用于基本類型,但對象、數組、NULL均返回”object”;2. instanceof通過原型鏈判斷實例,但不能跨iframe;3. constructor指向創建對象的構造函數,但易被修改;4. object.prototype.tostring.call()最可靠,可識別所有類型且不受iframe影響。通用類型判斷函數可用object.prototype.tostring.call()配合slice提取類型名。判斷數組推薦使用Array.isarray(),判斷null和undefined應使用嚴格相等運算符。判斷對象需排除null和數組。

JavaScript中如何判斷數據類型?

JavaScript判斷數據類型,說簡單也簡單,說復雜也挺復雜。核心在于理解typeof、instanceof、constructor以及Object.prototype.toString.call()這幾個家伙的特性,然后根據實際場景選擇最合適的。

JavaScript中如何判斷數據類型?

typeof 運算符

JavaScript中如何判斷數據類型?

typeof 能快速區分一些基本類型,比如:

立即學習Java免費學習筆記(深入)”;

  • typeof “hello” // “string”
  • typeof 123 // “number
  • typeof true // “Boolean
  • typeof undefined // “undefined”
  • typeof symbol() // “symbol” (es6新增)
  • typeof 123n // “bigint” (ES2020新增)

但它有個致命的缺陷:對于對象,它一律返回 “object”。這包括數組、null,甚至是你自己創建的類實例。

JavaScript中如何判斷數據類型?

typeof {} // "object" typeof [] // "object" typeof null // "object"  <-- 這是一個歷史遺留bug

instanceof 運算符

instanceof用來判斷一個對象是否是某個構造函數的實例。它通過原型鏈向上查找,如果找到對應的構造函數的prototype屬性,就返回true。

let arr = []; arr instanceof Array // true arr instanceof Object // true  <-- 因為Array的原型鏈上也有Object  function Person(){} let p = new Person(); p instanceof Person // true

instanceof的問題在于,它不能跨iframe判斷,因為不同的iframe有各自的全局環境和構造函數。

constructor 屬性

每個對象都有一個constructor屬性,指向創建該對象的構造函數。

let arr = []; arr.constructor === Array // true let num = 123; num.constructor === Number // true

但constructor屬性是可以被修改的,所以不完全可靠。

Object.prototype.toString.call() 方法

這被認為是JavaScript中判斷數據類型最可靠的方法。它通過改變this指向,讓Object.prototype.toString方法作用于不同的數據類型,從而返回一個表示該類型信息的字符串

Object.prototype.toString.call("hello") // "[object String]" Object.prototype.toString.call(123) // "[object Number]" Object.prototype.toString.call(true) // "[object Boolean]" Object.prototype.toString.call(undefined) // "[object Undefined]" Object.prototype.toString.call(null) // "[object Null]" Object.prototype.toString.call({}) // "[object Object]" Object.prototype.toString.call([]) // "[object Array]" Object.prototype.toString.call(new Date()) // "[object Date]" Object.prototype.toString.call(function(){}) // "[object Function]"

這個方法幾乎可以判斷所有數據類型,而且不受iframe的影響。

如何編寫一個通用的類型判斷函數?

function typeOf(obj) {   let type = Object.prototype.toString.call(obj).slice(8, -1);   return type.toLowerCase(); }  typeOf("hello"); // "string" typeOf(123); // "number" typeOf([]); // "array" typeOf(null); // "null"

JavaScript中如何判斷數組?

除了上面提到的方法,判斷數組還有一些更簡潔的方式。

  1. Array.isArray():這是ES5新增的方法,專門用來判斷一個值是否為數組,簡單直接,推薦使用。

    Array.isArray([]) // true Array.isArray({}) // false
  2. instanceof:前面已經提到,可以用instanceof Array來判斷。

  3. Object.prototype.toString.call():這是最通用的方法,前面也已經詳細介紹。

Object.prototype.toString.call([]) === '[object Array]' // true

JavaScript中如何判斷null和undefined?

  • typeof:typeof null 返回 “object”,typeof undefined 返回 “undefined”。
  • 嚴格相等運算符(===):這是最直接可靠的方式。
let a = null; let b; // undefined  a === null // true b === undefined // true

需要注意的是,null == undefined 返回 true,這是因為它們在進行相等比較時會進行類型轉換。所以,要區分 null 和 undefined,必須使用嚴格相等運算符。

如何判斷一個變量是否是對象?

判斷一個變量是否是對象,需要排除 null 和數組。

function isObject(obj) {   return typeof obj === 'object' && obj !== null && !Array.isArray(obj); }  isObject({}); // true isObject([]) // false isObject(null) // false isObject(123) // false

這段代碼首先判斷typeof obj === ‘object’,這可以排除基本類型。然后,obj !== null 排除了 null,!Array.isArray(obj) 排除了數組。

? 版權聲明
THE END
喜歡就支持一下吧
點贊8 分享