MST

星途 面试题库

面试题:JavaScript 中如何准确判断数据类型及优化判断方法

在JavaScript中,有多种方法可判断数据类型,如typeof、instanceof等。请列举至少3种判断数据类型的方法,并说明在判断对象类型时,如何优化判断过程以确保准确性,避免误判。例如,判断一个变量是否为数组,怎样做可以在不同环境下都得到正确结果。
10.4万 热度难度
编程语言JavaScript

知识考点

AI 面试

面试题答案

一键面试
  1. typeof
    • 说明typeof操作符返回一个字符串,表示未经计算的操作数的类型。例如:
    console.log(typeof 123); // "number"
    console.log(typeof "abc"); // "string"
    console.log(typeof true); // "boolean"
    console.log(typeof null); // "object"(这是JavaScript的一个历史遗留问题)
    console.log(typeof undefined); // "undefined"
    console.log(typeof function(){}); // "function"
    
    • 局限性typeof对于对象类型(除函数外)都返回"object",无法准确区分具体的对象类型,如数组、普通对象等。
  2. instanceof
    • 说明instanceof用于检测构造函数的prototype属性是否出现在某个实例对象的原型链上。例如:
    function Animal(){}
    function Dog(){}
    Dog.prototype = new Animal();
    const myDog = new Dog();
    console.log(myDog instanceof Dog); // true
    console.log(myDog instanceof Animal); // true
    const arr = [];
    console.log(arr instanceof Array); // true
    
    • 局限性:在跨窗口(如iframe)等环境下,不同窗口的构造函数是不同的,instanceof可能会误判。比如在一个iframe中有一个数组,在主窗口使用instanceof Array判断该iframe中的数组可能会返回false
  3. Object.prototype.toString.call()
    • 说明:这是一种较为可靠的判断数据类型的方法。Object.prototype.toString方法返回一个表示对象类型的字符串。通过call方法改变this指向,可以准确获取不同数据类型的字符串标识。例如:
    console.log(Object.prototype.toString.call(123)); // "[object Number]"
    console.log(Object.prototype.toString.call("abc")); // "[object String]"
    console.log(Object.prototype.toString.call(true)); // "[object Boolean]"
    console.log(Object.prototype.toString.call(null)); // "[object Null]"
    console.log(Object.prototype.toString.call(undefined)); // "[object Undefined]"
    console.log(Object.prototype.toString.call(function(){})); // "[object Function]"
    console.log(Object.prototype.toString.call([])); // "[object Array]"
    console.log(Object.prototype.toString.call({})); // "[object Object]"
    
    • 优点:这种方法不受跨窗口等环境影响,能准确判断各种数据类型。

判断对象类型时优化判断过程以确保准确性

  1. 判断数组
    • 使用Array.isArray():这是ES5新增的方法,专门用于判断一个值是否为数组。它能在不同环境下准确判断。例如:
    const arr = [];
    console.log(Array.isArray(arr)); // true
    const notArr = {};
    console.log(Array.isArray(notArr)); // false
    
    • 使用Object.prototype.toString.call():在ES5之前,可以使用Object.prototype.toString.call()来判断数组,它同样不受环境影响。例如:
    const arr = [];
    console.log(Object.prototype.toString.call(arr) === "[object Array]"); // true
    
  2. 判断其他对象类型
    • 使用Object.prototype.toString.call():对于判断普通对象、日期对象、正则对象等,Object.prototype.toString.call()是一个很好的选择。例如:
    const date = new Date();
    console.log(Object.prototype.toString.call(date) === "[object Date]"); // true
    const reg = /abc/;
    console.log(Object.prototype.toString.call(reg) === "[object RegExp]"); // true
    const obj = {};
    console.log(Object.prototype.toString.call(obj) === "[object Object]"); // true
    
    • 避免使用typeof判断对象具体类型:因为typeof对于对象类型(除函数外)都返回"object",容易误判,尽量不用于具体对象类型的判断。
    • 注意instanceof的局限性:在跨窗口等环境下使用instanceof判断对象类型时要小心,优先使用更可靠的Object.prototype.toString.call()或专门的判断方法(如Array.isArray())。