typeof 和 instanceof有什么区别

typeofinstanceof 都是 JavaScript 中用于类型检查的操作符,但它们的用途和工作方式不同:

1. typeof 操作符

作用 :返回一个字符串,表示未经计算的操作数的类型。

js 复制代码
typeof 42;              // "number"
typeof "hello";         // "string"
typeof true;            // "boolean"
typeof undefined;       // "undefined"
typeof null;            // "object" (历史遗留bug)
typeof {};              // "object"
typeof [];              // "object"
typeof function() {};   // "function"
typeof Symbol();        // "symbol"
typeof 123n;            // "bigint"

特点:

  • 主要用于判断基本数据类型
  • 对于对象类型,除了函数返回 "function",其他都返回 "object"
  • typeof null 返回 "object" 是著名的 JavaScript 设计缺陷

2. instanceof 操作符

作用 :检查构造函数的 prototype 属性是否出现在对象的原型链上。

js 复制代码
[] instanceof Array;           // true
[] instanceof Object;          // true
({}) instanceof Object;        // true
new Date() instanceof Date;    // true

function Person() {}
const p = new Person();
p instanceof Person;           // true
p instanceof Object;           // true

特点:

  • 主要用于判断对象的具体类型(是哪个类的实例)
  • 沿着原型链向上查找
  • 只能用于对象,对基本数据类型无效

主要区别

特性 typeof instanceof
返回类型 字符串 布尔值
主要用途 判断基本数据类型 判断对象的具体类型
对基本类型 有效 无效(返回 false)
对null 返回 "object" 报错(null不是对象)
对数组 返回 "object" 可判断 Array
原理 检查值的类型标签 检查原型链

使用示例对比

js 复制代码
// 基本类型
typeof 123;           // "number"
123 instanceof Number; // false (原始类型)

typeof "abc";         // "string"
"abc" instanceof String; // false

// 对象类型
typeof [];            // "object"
[] instanceof Array;  // true

typeof {};            // "object"
{} instanceof Object; // true

// 函数
typeof function() {}; // "function"
(function() {}) instanceof Function; // true

// 特殊案例
typeof null;          // "object" ❌
null instanceof Object; // false

const num = new Number(123);
typeof num;           // "object"
num instanceof Number; // true

实际应用建议

  1. 判断基本类型 :使用 typeof
js 复制代码
if (typeof value === 'string') { /* ... */ }
  1. 判断数组 :使用 Array.isArray()(最可靠)
js 复制代码
Array.isArray([]);  // true
  1. 判断自定义对象类型 :使用 instanceof
js 复制代码
if (obj instanceof MyClass) { /* ... */ }
  1. 综合判断:可以结合使用
js 复制代码
function getType(value) {
  if (value === null) return 'null';
  if (Array.isArray(value)) return 'array';
  const type = typeof value;
  if (type !== 'object') return type;
  return value.constructor.name.toLowerCase();
}

注意事项

  1. instanceof 在跨框架(iframe)时可能不可靠,因为不同全局环境的构造函数不同
  2. 使用 Symbol.hasInstance 可以自定义 instanceof 的行为
  3. 对于精确的类型判断,可以使用 Object.prototype.toString.call()
js 复制代码
Object.prototype.toString.call([]);  // "[object Array]"
Object.prototype.toString.call({});  // "[object Object]"
相关推荐
saber_andlibert1 天前
TCMalloc底层实现
java·前端·网络
逍遥德1 天前
如何学编程之01.理论篇.如何通过阅读代码来提高自己的编程能力?
前端·后端·程序人生·重构·软件构建·代码规范
冻感糕人~1 天前
【珍藏必备】ReAct框架实战指南:从零开始构建AI智能体,让大模型学会思考与行动
java·前端·人工智能·react.js·大模型·就业·大模型学习
程序员agions1 天前
2026年,“配置工程师“终于死绝了
前端·程序人生
alice--小文子1 天前
cursor-mcp工具使用
java·服务器·前端
晚霞的不甘1 天前
揭秘 CANN 内存管理:如何让大模型在小设备上“轻装上阵”?
前端·数据库·经验分享·flutter·3d
小迷糊的学习记录1 天前
0.1 + 0.2 不等于 0.3
前端·javascript·面试
梦帮科技1 天前
Node.js配置生成器CLI工具开发实战
前端·人工智能·windows·前端框架·node.js·json
VT.馒头1 天前
【力扣】2695. 包装数组
前端·javascript·算法·leetcode·职场和发展·typescript
css趣多多1 天前
一个UI内置组件el-scrollbar
前端·javascript·vue.js