js中如何对数据类型进行区分判断?有几种方法?

1.typeof

返回值:返回值包括 "undefined", "boolean", "number", "string", "bigint", "symbol", "object" 和 "function"。
需要注意的就是typeof无法区分引用数据类型(对象和数组),且对 null 的判断为object

typeof 42; // "number"
typeof "Hello"; // "string"
typeof true; // "boolean"
typeof undefined; // "undefined"
typeof Symbol("id"); // "symbol"
typeof function() {}; // "function"
typeof NaN; // "number"
typeof null; // "object" (这是 JavaScript 的一个历史遗留问题)
typeof {}; // "object"
typeof []; // "object"


2.instanceof 操作符
返回值:返回布尔值 true 或 false
需要注意的是instanceof 适用于引用数据类型,如对象、数组、函数等。对于基本数据类型(如NumberStringBoolean等),instanceof 并不能正确区分。

let arr = [];
arr instanceof Array; // true
let obj = {};
obj instanceof Object; // true
function Person(name) {
    this.name = name;
}
let person = new Person("John");
person instanceof Person; // true

// 对于基本数据类型,instanceof 的行为不如预期:
let num = 42;
console.log(num instanceof Number); // false
let str = "Hello";
console.log(str instanceof String); // false
let bool = true;
console.log(bool instanceof Boolean); // false

3.Object.prototype.toString.call() 方法

用法:使用 Object.prototype.toString.call() 方法可以获取对象的内部属性 [[Class]] 的值,从而准确判断其类型。
返回值:返回一个以 [object  开头、] 结尾的字符串。

Object.prototype.toString.call(42); // "[object Number]"
Object.prototype.toString.call("Hello"); // "[object String]"
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(function() {}); // "[object Function]"
Object.prototype.toString.call(Symbol("id")); // "[object Symbol]"

小结

  • 基本数据类型:使用typeofObject.prototype.toString.call()来区分。
  • 引用数据类型:使用instanceofObject.prototype.toString.call()来区分。


小补充:
constructor

(2).constructor === Number// true
([]).constructor === Array // true
隐患:
constructor代表的是构造函数指向的类型,可以被修改的js
function Fn(){}
Fn.prototype = new Array();
var f = new Fn();
// 在这种情况下,f的constructor属性不再指向Fn,而是指向 Array,因为 Fn.prototype 被设置为一个 Array 实例
// 在使用 JavaScript 中的原型继承时,要特别注意 constructor 属性可能带来的不准确性,并在必要时手动重置它以确保类型检测的可靠性。对于类型检测,Object.prototype.toString.call() 方法更为可靠,因为它不受 constructor 属性修改的影响。

相关推荐

  1. js判断对象是否为空的方法

    2024-07-19 17:12:01       38 阅读
  2. js类型判断方法总结

    2024-07-19 17:12:01       33 阅读
  3. 前端判断数据类型方法哪些?

    2024-07-19 17:12:01       26 阅读
  4. 【 React 】React 引入css的方式区别

    2024-07-19 17:12:01       42 阅读
  5. SQL Server ,删除表数据以下方式

    2024-07-19 17:12:01       58 阅读
  6. 数据类型判断方法

    2024-07-19 17:12:01       158 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-19 17:12:01       70 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-19 17:12:01       74 阅读
  3. 在Django里面运行非项目文件

    2024-07-19 17:12:01       62 阅读
  4. Python语言-面向对象

    2024-07-19 17:12:01       72 阅读

热门阅读

  1. 【C++】C++中find_first_of函数解析

    2024-07-19 17:12:01       21 阅读
  2. PHP MySQL 读取数据

    2024-07-19 17:12:01       19 阅读
  3. Handler续谈(epoll)

    2024-07-19 17:12:01       19 阅读
  4. Git提交到错误分支怎么办?(解决办法)

    2024-07-19 17:12:01       22 阅读
  5. 在ubuntu系统上安装qt 2

    2024-07-19 17:12:01       20 阅读
  6. CONFIG_MTD_SPI_NOR_USE_4K_SECTORS

    2024-07-19 17:12:01       23 阅读
  7. 网络通信协议

    2024-07-19 17:12:01       14 阅读
  8. opencv 使用XML和YAML格式来输入输出文件

    2024-07-19 17:12:01       26 阅读
  9. css2024

    2024-07-19 17:12:01       21 阅读
  10. Jangow

    Jangow

    2024-07-19 17:12:01      23 阅读
  11. new一个对象的具体步骤

    2024-07-19 17:12:01       24 阅读