蓝戒博客

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

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

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

最新 热点 随机
最新 热点 随机
Vue 3.6「无虚拟 DOM」时代开启:深入解读 Vapor Mode 的革命性变革 2025 最推荐的 uni-app 技术栈:unibest + uView Pro 高效开发全攻略 前端开源工具 PinMe:极简部署体验分享 架构评估方法 ATAM:系统性洞察架构质量的利器 软件系统架构评估与质量属性分析 AI 大模型开发:如何实现数据向量化
Webpack 实战:Code Splitting 优化页面加载性能前端开源工具 PinMe:极简部署体验分享AI 产品前端技术全解析:模型可视化与流式响应实践前端内存泄露防范及编码攻略DApp开发前端技术全解析:技术选型、功能实现与开发步骤Web Workers:释放浏览器多线程的魔力
云端无服时代:Serverless 架构的进化、现实与未来 javascript中DOM0,DOM2,DOM3级事件模型解析 display:inline|block|inline-block的区别及特点 Vue 2 安全漏洞深度解析与修复:CVE-2024-9506 & CVE-2024-6783 浏览器消息通知库:iNotify.js 【视频】乔布斯:遗失的访谈(1995)
最近评论
渔夫 发布于 6 天前(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