蓝戒博客

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

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

2016年8月15日 10520点热度 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 中对话未来。

最新 热点 随机
最新 热点 随机
《生化危机》女主手搓AI记忆系统,48小时狂揽7千星!AI的长期记忆终于有解了? 用一条自然语言指令,让AI自动完成了调研、写稿、配音、剪辑全流程 你以为 AI 配音还在拼“像不像”,结果有人已经把“整个语音工作室”开源了 Claude Opus 4.7 上线:编程能力炸裂式跃升,Anthropic 手握更强模型却故意不发布 Gemma 4发布4天即遭"完全越狱",开源AI的安全与自由之争 一个文件让AI写代码不再"翻车":45K星的Karpathy指南火了
Dan Koe:不想打工?用这套方法把兴趣变成收入GitHub 爆火 4 万星项目:MiroFish,到底是 AI 新神话,还是下一代预测引擎DeerFlow 2.0:字节跳动开源的超级智能体框架,让AI研究、编码、创作一气呵成!Claude Code 生态大爆发:这周 GitHub 热点,已经不是工具升级,而是工作方式重写我把 Codex CLI 装上了“外挂大脑”:oh-my-codex 到底有多猛?别再盲下大模型了:用 llmfit 一秒看懂你的电脑到底能跑谁
双因子认证(2FA)的实现方案与最佳实践 package.json配置字段全解析 55个AI专家帮你打工:Agency-Agents让OPC(一人公司)成为现实 Interact.js:一个轻量级且强大的拖拽、缩放与手势库 一匹"快乐马"闯进AI视频赛道,2026年视频生成格局彻底变天? 网络应用程序架构设计:遵循12-Factor方法论
最近评论
渔夫 发布于 6 个月前(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