欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript 類型檢查操作符typeof 和 instanceof 的區(qū)別對比

 更新時間:2025年08月23日 11:48:04   作者:_璐-  
本文給大家介紹JavaScript類型檢查操作符typeof和instanceof的區(qū)別,本文結(jié)合實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

typeofinstanceof 都是 JavaScript 中用于類型檢查的操作符,但它們的工作方式和應(yīng)用場景有顯著不同。

1. 基本區(qū)別對比

特性typeofinstanceof
操作目標(biāo)適用于所有 JavaScript 值只適用于對象
返回值返回類型名稱的字符串返回布爾值
檢查內(nèi)容檢查值的類型檢查對象的原型鏈
對原始值的處理有效總是返回 false
對數(shù)組的檢查返回 "object"arr instanceof Array → true
對 null 的檢查返回 "object"(歷史遺留問題)null instanceof Object → false
跨窗口/框架問題無影響可能有影響

2. 詳細區(qū)別解釋

2.1 工作原理不同

typeof:

  • 返回一個表示操作數(shù)類型的字符串
  • 對于原始值直接返回對應(yīng)的類型名稱
  • 對于對象,通常返回 "object"(函數(shù)返回 "function")
typeof 42;           // "number"
typeof "hello";      // "string"
typeof true;         // "boolean"
typeof undefined;    // "undefined"
typeof null;         // "object" (歷史遺留問題)
typeof {};           // "object"
typeof [];           // "object"
typeof function(){}; // "function"

instanceof:

  • 檢查構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在對象的原型鏈中
  • 只對對象有效,原始值總是返回 false
[] instanceof Array;      // true
[] instanceof Object;     // true
new Date() instanceof Date; // true
?
42 instanceof Number;     // false
"str" instanceof String;  // false

2.2 對原始值的處理

typeof 可以正確處理原始值:

typeof 42;        // "number"
typeof "hello";   // "string"
typeof true;      // "boolean"
typeof undefined; // "undefined"
typeof null;      // "object" (這是唯一例外)

instanceof 對原始值總是返回 false:

42 instanceof Number;        // false
"hello" instanceof String;   // false
true instanceof Boolean;     // false

2.3 對數(shù)組的檢查

typeof 無法區(qū)分數(shù)組和普通對象:

typeof [];      // "object"
typeof {};      // "object"

instanceof 可以檢測數(shù)組:

[] instanceof Array;    // true
{} instanceof Array;    // false

2.4 對 null 的檢查

typeof 的著名陷阱:

typeof null;  // "object" (這是歷史遺留問題)

instanceof 正確處理 null:

null instanceof Object;  // false

2.5 繼承關(guān)系的檢查

instanceof 可以檢查繼承關(guān)系:

class Animal {}
class Dog extends Animal {}
let dog = new Dog();
dog instanceof Dog;     // true
dog instanceof Animal;  // true

typeof 無法檢查繼承關(guān)系:

typeof dog;  // "object" (無法知道是Dog還是Animal)

3. 使用場景對比

適合使用typeof的情況

檢查變量是否已定義:

if (typeof variable === 'undefined') {
  // 變量未定義
}

檢查基本類型:

function add(a, b) {
  if (typeof a !== 'number' || typeof b !== 'number') {
    throw new Error('參數(shù)必須是數(shù)字');
  }
  return a + b;
}

區(qū)分函數(shù)和其他對象:

if (typeof callback === 'function') {
  callback();
}

適合使用instanceof的情況

檢查對象的具體類型:

if (value instanceof Date) {
  console.log(value.getFullYear());
}

檢查自定義類的實例:

class User {}
let user = new User();
if (user instanceof User) {
  // 處理用戶對象
}

檢查繼承關(guān)系:

if (dog instanceof Animal) {
  // 處理動物對象
}

4. 特殊注意事項

4.1 跨窗口/框架問題

instanceof 在不同 iframe 或窗口之間可能不可靠:

// 假設(shè)array來自另一個iframe
let iframeArray = window.frames[0].Array;
let arr = new iframeArray(1, 2, 3);
console.log(arr instanceof Array); // false

解決方法:

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

4.2 構(gòu)造函數(shù)原型被修改

修改構(gòu)造函數(shù)的原型會影響 instanceof 的結(jié)果:

function Foo() {}
let foo = new Foo();
console.log(foo instanceof Foo); // true
// 修改原型
Foo.prototype = {};
console.log(foo instanceof Foo); // false

4.3 手動實現(xiàn)類型檢查

對于更復(fù)雜的類型檢查,可以結(jié)合使用:

function getType(obj) {
  if (obj === null) return "null";
  if (Array.isArray(obj)) return "array";
  if (obj instanceof Date) return "date";
  return typeof obj;
}
console.log(getType([]));      // "array"
console.log(getType(null));     // "null"
console.log(getType(new Date())); // "date"
console.log(getType(42));       // "number"

5. 總結(jié)對比表

場景typeofinstanceof推薦方案
檢查基本類型?? 優(yōu)秀? 無效typeof
檢查數(shù)組? 不足?? 可用Array.isArray()
檢查 null? 陷阱?? 正確obj === null
檢查函數(shù)?? 優(yōu)秀? 無效typeof
檢查自定義類實例? 不足?? 優(yōu)秀instanceof
檢查繼承關(guān)系? 無效?? 優(yōu)秀instanceof
檢查跨窗口對象?? 可用? 不可靠特定API(如Array.isArray()

6. 最佳實踐建議

  • 優(yōu)先使用專用方法
    • 檢查數(shù)組用 Array.isArray()
    • 檢查 null 用 obj === null
  • 組合使用
function isNumber(value) {
  return typeof value === 'number' || value instanceof Number;
}
  • 理解局限性
    • typeof null 返回 "object"
    • instanceof 對原始值無效
    • 跨窗口對象檢查問題
  • 考慮使用現(xiàn)代類型檢查
    • TypeScript 提供編譯時類型檢查
    • 使用 Object.prototype.toString.call() 更精確
    • 通過理解 typeofinstanceof 的區(qū)別和適用場景,你可以更準確地編寫類型檢查邏輯,避免常見的 JavaScript 類型陷阱。

到此這篇關(guān)于JavaScript 類型檢查操作符typeof 和 instanceof 的區(qū)別詳解的文章就介紹到這了,更多相關(guān)js typeof 和 instanceof 區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • js中如何對url進行編碼和解碼

    js中如何對url進行編碼和解碼

    這篇文章主要介紹了js中如何對url進行編碼和解碼問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • Bootstrap?按鈕下拉菜單的實現(xiàn)示例

    Bootstrap?按鈕下拉菜單的實現(xiàn)示例

    本文主要介紹了Bootstrap?按鈕下拉菜單的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-07-07
  • jquery獲取URL中參數(shù)解決中文亂碼問題的兩種方法

    jquery獲取URL中參數(shù)解決中文亂碼問題的兩種方法

    從A頁面通過url傳參到B頁面時,獲取URL中參數(shù)出現(xiàn)中文亂碼問題,解析url參數(shù)的正確方法如下,感興趣的朋友可以參考下
    2013-12-12
  • js結(jié)合正則實現(xiàn)國內(nèi)手機號段校驗

    js結(jié)合正則實現(xiàn)國內(nèi)手機號段校驗

    這篇文章主要介紹了js結(jié)合正則實現(xiàn)國內(nèi)手機號段校驗的方法以及使用js和jQuery實現(xiàn)的簡單校驗手機號的示例,非常簡單實用,有需要的小伙伴可以參考下。
    2015-06-06
  • JavaScript中forEach和map的使用場景

    JavaScript中forEach和map的使用場景

    本文JavaScript中forEach和map的使用場景,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-05-05
  • JavaScript限定復(fù)選框的選擇個數(shù)示例代碼

    JavaScript限定復(fù)選框的選擇個數(shù)示例代碼

    有10個復(fù)選框,用戶最多只能勾選3個,否則就灰掉所有復(fù)選框,具體實現(xiàn)思路及代碼如下,感興趣的朋友可以參考下,希望對大家有所幫助
    2013-08-08
  • undefined與null的區(qū)別示例詳解

    undefined與null的區(qū)別示例詳解

    這篇文章主要為大家介紹了undefined與null的區(qū)別示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • JS 模塊化語法 import、export詳解

    JS 模塊化語法 import、export詳解

    這篇文章主要介紹了JS 模塊化語法 import、export詳解,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2025-05-05
  • 微信小程序使用webview打開pdf文檔以及顯示網(wǎng)頁內(nèi)容的方法步驟

    微信小程序使用webview打開pdf文檔以及顯示網(wǎng)頁內(nèi)容的方法步驟

    在線查看PDF文件,已經(jīng)是很常見的需求了,下面這篇文章主要給大家介紹了關(guān)于微信小程序使用webview打開pdf文檔以及顯示網(wǎng)頁內(nèi)容的方法步驟,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2022-07-07
  • Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應(yīng)屬性

    Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應(yīng)屬性

    Markdown 是一種輕量級的標(biāo)記語言,它的優(yōu)點很多,目前也被越來越多的寫作愛好者,撰稿者廣泛使用。接下來通過本文給大家介紹Markdown與Bootstrap相結(jié)合實現(xiàn)圖片自適應(yīng)屬性,感興趣的朋友一起學(xué)習(xí)吧
    2016-05-05

最新評論