javascript如何检测数据类型_typeof和instanceof有何区别【教程】
技术百科
紅蓮之龍
发布时间:2026-01-28
浏览: 次 typeof 对 null 返回 "object"(历史 bug),对数组、普通对象、正则、日期等均返回 "object",仅对函数返回 "function";instanceof 依赖原型链,跨 iframe 失效且无法检测原始值。
typeof 适合快速判断基本类型,但对 null 和对象(包括数组、正则、日期等)一律返回 "object";instanceof 可识别引用类型的构造器来源,但依赖原型链,跨 iframe 失效。
typeof 对哪些值返回意外结果?
它在几个关键地方“不准”:
-
typeof null返回"object"(历史 bug,已成标准) -
typeof []、typeof {}、typeof /reg/、typeof全是
new Date()
"object" -
typeof function() {}返回"function"(这是特例,不是标准对象) -
typeof Symbol()返回"symbol",typeof BigInt(1)返回"bigint"(ES6+ 新增,表现正常)
instanceof 的原理和典型失效场景
它本质是检查 left.__proto__ 是否沿原型链能找到 right.prototype。因此:
- 能正确区分
[] instanceof Array(true)、{} instanceof Object(true)、/a/ instanceof RegExp(true) - 但
let iframe = document.createElement('iframe'); document.body.appendChild(iframe); let arr = iframe.contentWindow.Array; [] instanceof arr返回false(不同全局环境,原型链断裂) - 无法检测原始值:
123 instanceof Number是false(数字字面量不是实例)
更可靠的类型检测方案(推荐组合)
单靠一个操作符不够,常用补救方式:
- 判空先用
value === null,再用typeof value === "object"避开null陷阱 - 检测数组优先用
Array.isArray(value)(最准,ES5+ 支持) - 检测内置对象统一走
Object.prototype.toString.call(value):
Object.prototype.toString.call([]) // "[object Array]"
Object.prototype.toString.call(/a/) // "[object RegExp]"
Object.prototype.toString.call(null) // "[object Null]" - 自定义类可用
instanceof,但注意避免跨上下文使用
真正难的不是记住区别,而是意识到:JavaScript 没有“类型系统”的强契约,所有检测都是基于运行时结构的推测——所以别依赖单一手段,尤其在处理第三方数据或跨环境对象时。
# 几个
# 意识到
# 都是
# 它在
# 这是
# 自定义
# 能找到
# app
# 已成
# 再用
# win
# 对象
# javascript
# java
# 区别
# typeof
# function
# bug
# NULL
# 数据类型
# 引用类型
# Object
# date
# Array
# 但对
# number
# es6
# symbol
# regexp
# prototype
# iframe
相关栏目:
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
AI推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
SEO优化<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
技术百科<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
谷歌推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
百度推广<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
网络营销<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
案例网站<?muma echo $count; ?>
】
<?muma
$count = M('archives')->where(['typeid'=>$field['id']])->count();
?>
【
精选文章<?muma echo $count; ?>
】
相关推荐
- 如何在 VS Code 中正确配置并使用 NumP
- Win11怎么关闭OneDrive同步_Win11
- Win11局域网共享怎么设置 Win11文件夹网络
- php与c语言在嵌入式中有何区别_对比两者在硬件控
- Win11讲述人怎么关闭_Win11误触开启语音朗
- Python模块的__name__属性如何由导入方
- Go 语言标准库为何不提供泛型 Contains
- Win11怎么关闭内容自适应亮度_Windows1
- PythonPandas数据分析项目教程_时间序列
- 如何使用Golang管理模块版本_Golanggo
- PHP主流架构如何做单元测试_工具与流程【详解】
- Windows10电脑怎么查看硬盘通电时间_Win
- PHP主流架构怎么部署到Docker_容器化流程【
- VSC里PHP变量未定义报错怎么解决_错误抑制技巧
- Linux如何安装JDK11_Linux环境变量配
- Mac怎么设置登录项_Mac管理开机自启动程序【教
- php在Linux怎么部署_LNMP环境搭建PHP
- 如何使用Golang理解结构体指针方法接收者_Go
- Win11怎么把图标拖到任务栏_Win11固定应用
- 如何使用Golang捕获测试日志_Golang t
- 如何解决Windows时间不准的问题?(自动同步设
- Ajax提交表单PHP怎么接收_处理Ajax发送的
- Python lxml的etree和Element
- PythonPandas数据分析教程_数据清洗与处
- MAC的“接续互通”功能无法使用怎么办_MAC检查
- XAMPP 启动失败(Apache 突然停止)的终
- Win11怎么设置闹钟_Windows 11时钟应
- Win10系统映像怎么恢复 Win10使用系统映像
- Python解释执行模型_字节码流程说明【指导】
- Win11怎么关闭防火墙通知_屏蔽Win11安全中
- Win11摄像头无法使用怎么办_Win11相机隐私
- Win11应用商店下载慢怎么办 Win11更改DN
- 短链接怎么用php还原_从基础原理到代码实现教学【
- Win11如何连接Xbox手柄 Win11蓝牙连接
- Go语言中CookieJar的持久化机制解析:内存
- Win11怎样彻底卸载自带应用_Win11彻底卸载
- Windows10系统怎么查看IP地址_Win10
- PythonGIL机制理解_多线程限制解析【教程】
- Win10怎么卸载金山毒霸_Win10彻底卸载金山
- VSC怎样用终端运行PHP_命令行执行脚本的步骤【
- PHP的Workerman对架构扩展有啥帮助_应用
- 如何使用Golang实现多重错误处理_Golang
- Linux如何安装Tomcat应用服务器_Linu
- 如何在 Django 中修改用户密码后保持会话不丢
- Windows10电脑怎么设置防火墙出站规则_Wi
- c++中如何使用std::variant_c++1
- c++获取当前时间戳_c++ time函数使用详解
- Win10怎么创建桌面快捷方式 Win10为应用创
- Windows10如何更改系统字体大小_Win10
- Win11怎么更改计算机名_Windows11系统


QQ客服