蓝戒博客

  • 首页
  • 研发说
  • 架构论
  • 效能录
  • AI谈
  • 随笔集
智构苍穹
AI为翼,架构为骨,文化为魂,实践探新境,价值筑长青。
  1. 首页
  2. 研发说
  3. 正文

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

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

最新 热点 随机
最新 热点 随机
npm 安全更新:把握令牌变更与发布体系的迁移参考指南 TresJS:用 Vue 构建现代化交互式 3D 体验 i18n 高效实现方案:前端国际化神器安利一波 前端国际化 i18n 实践:从项目到组件库的全链路方案 GEO(生成引擎优化)完整指南:AI 搜索时代的企业内容新机会 NativeScript:用 JavaScript / TypeScript 构建真正的原生应用
前端开源工具 PinMe:极简部署体验分享大屏适配的核心痛点与一行 autofit 解决方案markdown-exit:现代化的 Markdown 解析工具Lerna + Monorepo:前端多仓库管理的最佳实践CrewAI:基于角色协作的 AI Agent 团队框架浅析2025 最推荐的 uni-app 技术栈:unibest + uView Pro 高效开发全攻略
手机端rem适配方案 前端开源工具 PinMe:极简部署体验分享 移动端HTML5中video视频播放优化方案 构建高效 AI 工作流的工程化实践 SlipJs高效实现移动web页面整屏滑动 DApp开发前端技术全解析:技术选型、功能实现与开发步骤
最近评论
渔夫 发布于 1 个月前(11月05日) 学到了,感谢博主分享
沙拉小王子 发布于 8 年前(11月30日) 适合vue入门者学习,赞一个
沙拉小王子 发布于 8 年前(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