蓝戒博客

  • 首页
  • 研发说
  • 架构论
  • 效能录
  • AI谈
  • 随笔集
智构苍穹
融合 AI、架构与工程实践,沉淀方法论,构建可持续的技术价值。
  1. 首页
  2. 研发说
  3. 正文

解析Object.prototype.toString.call()进行数据类型判断

2016年8月15日 10827点热度 0人点赞 0条评论

首先看一段ECMA中对Object.prototype.toString的解释:

1.If the this value is undefined, return "[object Undefined]".
2.If the this value is null, return "[object Null]".
3.Let O be the result of calling ToObject passing the this value as the argument.
4.Let class be the value of the [[Class]] internal property of O.
5.Return the String value that is the result of concatenating the three Strings "[object ", class, and "]".

翻译:
1.如果this的值为undefined,则返回"[object Undefined]".
2.如果this的值为null,则返回"[object Null]".
3.让O成为调用ToObject(this)的结果.
4.让class成为O的内部属性[[Class]]的值.
5.返回三个字符串"[object ", class, 以及 "]"连接后的新字符串.

查看Jquery就会发现,jquery就是是通过Object.prototype.toString.call还判断对象的类型的,那究竟如何做呢?
我们先来了解一下call方法:

function a(){
this.aa=function(){
alert("a");
}
}
function b(){
alert(a.call(this));
}
var c=new b();
c.aa();

call方法可以让b复制a里面的方法来进行使用,也就是所谓的继承。

var obj = {name:"Tom", age:18};
console.log(typeof obj.toString()); //string
console.log(obj.toString()); //"[object Object]"

我们知道,Javascript中,一切皆为对象。所以如下代码,应当会输出对应字符:

var oP = Object.prototype,
toString = oP.toString;

console.log(toString.call([123]));//[object Array]
console.log(toString.call('123'));//[object String]
console.log(toString.call({a: '123'}));//[object Object]
console.log(toString.call(/123/));//[object RegExp]
console.log(toString.call(123));//[object Number]
console.log(toString.call(undefined));//[object Undefined]
console.log(toString.call(null));//[object Null]

标准浏览器中完美的作到,但是(为什么要说但是呢)IE6中,却会出现以下问题:
通过Object.prototype.toString.call获取的 字符串,undefined,null均为Object

所以,我们又要悲剧的先对以上类型进行判断,完整代码:

var oP = Object.prototype,
toString = oP.toString;

function typeOf(value) {
if (null === value) {
return 'null';
}

var type = typeof value;
if ('undefined' === type || 'string' === type) {
return type;
}

var typeString = toString.call(value);
switch (typeString) {
case '[object Array]':
return 'array';
case '[object Date]':
return 'date';
case '[object Boolean]':
return 'boolean';
case '[object Number]':
return 'number';
case '[object Function]':
return 'function';
case '[object RegExp]':
return 'regexp';
case '[object Object]':
if (undefined !== value.nodeType) {
if (3 == value.nodeType) {
return (/\S/).test(value.nodeValue) ? 'textnode': 'whitespace';
} else {
return 'element';
}
} else {
return 'object';
}
default:
return 'unknow';
}
}

参考文档:
1. https://segmentfault.com/n/1330000004202636
2. http://openxtiger.iteye.com/blog/1893378
3. http://my.oschina.net/sfm/blog/33197

标签: javascript toString
最后更新:2025年9月13日

cywcd

我始终相信,技术不仅是解决问题的工具,更是推动思维进化和创造价值的方式。从研发到架构,追求极致效能;在随笔中沉淀思考,于 AI 中对话未来。

打赏 点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

cywcd

我始终相信,技术不仅是解决问题的工具,更是推动思维进化和创造价值的方式。从研发到架构,追求极致效能;在随笔中沉淀思考,于 AI 中对话未来。

最新 热点 随机
最新 热点 随机
深扒 Google Gemini Spark 这位"24小时不下班"的AI管家 Google推出Code Wiki,把“屎山代码”秒变可聊天的百科全书! VideoLingo凭什么敢称“全自动的视频本地化流水线”? 首个超2万亿级开源模型Kimi K3震撼发布,大模型格局彻底变天了? 别再手写 AI Agent 了!谷歌开源 agents-cli,一键给你的 AI 助手加满神仙级技能点 ChatGPT 实时语音 vs Gemini Live:深度实测!谁才是地表最强“无痛嘴替”?
开发者集体起立!cc-switch更新:原生干掉外挂,协议、会话、技能全包圆了!一键生成百万播放短视频?被吹上天的RedditVideoMakerBot,到底是搞钱神器还是时间杀手?别再迷信传统 RAG 了!知识库新物种 SAG 强势登场,实时干掉大模型“幻觉”!别再死磕提示词了!Google Flow Agent 彻底颠覆 AI 视频,有手就能当导演剪映要收网了?狂割韭菜后,这款狂揽5.7万Star的开源神器砸了谁的饭碗!被Figma卡脖子?这款自托管的开源神仙工具:Penpot,才是设计与开发的终极解药!
谷歌突然放大招:Gemma 4,可能是今年最值得本地部署的开源AI大模型 Codex + Agent Browser:让 AI 精准还原前端 UI 的新范式(从设计稿到像素级实现) alova.js:重新定义前端 API 集成体验的请求框架 Vike 介绍与使用入门:一个站在 Vite 肩膀上的新一代前端框架 2026 AI Agent 六大趋势:普通人如何抓住这波"数字员工"红利? Vue 3.6「无虚拟 DOM」时代开启:深入解读 Vapor Mode 的革命性变革
最近评论
渔夫 发布于 9 个月前(11月05日) 学到了,感谢博主分享
沙拉小王子 发布于 9 年前(11月30日) 适合vue入门者学习,赞一个
沙拉小王子 发布于 9 年前(11月30日) 适合vue入门者学习,赞一个
cywcd 发布于 9 年前(04月27日) 请参考一下这篇文章http://www.jianshu.com/p/fa4460e75cd8
cywcd 发布于 9 年前(04月27日) 请参考一下这篇文章http://www.jianshu.com/p/fa4460e75cd8

COPYRIGHT © 2025 蓝戒博客_智构苍穹-专注于大前端领域技术生态. ALL RIGHTS RESERVED.

京ICP备12026697号-2