Object.prototype.toString.call() 区分对象类型(判断对象类型)
在 JavaScript 里使用 typeof 来判断数据类型,只能区分基本类型,即 “number”,”string”,”undefined”,”boolean”,”object” 五种。对于数组、对象来说,其关系错综复杂,使用 typeof 都会统一返回 “object” 字符串。
要想区别对象、数组单纯使用 typeof 是不行的。或者你会想到 instanceof 方法,例如下面这样:
var a = {};
var b = [];
var c = function () {}; //a b c 都是 Object 的实例
console.log(a instanceof Object) //true
console.log(b instanceof Object) //true
console.log(c instanceof Object) //true //只有 Array 类型的 b 才是 Array 的实例
console.log(a instanceof Array) //false
console.log(b instanceof Array) //true
console.log(c instanceof Array) //false //只有 Function 类型的 c 才是 Function 的实例
console.log(a instanceof Function) //false
console.log(b instanceof Function) //false
console.log(c instanceof Function) //true
从以上代码来看,要判断复合数据类型,可以如下判断:
//对象
(a instanceof Object) && !(a instanceof Function) && !(a instanceof Function)
//数组
(a instanceof Object) && (a instanceof Array)
//函数
(a instanceof Object) && (a instanceof Function)
更简便的方式,即是使用 Object.prototype.toString.call() 来确定类型,ECMA 5.1 中关于该方法的描述[1]是这样的:
- When the toString method is called, the following steps are taken:
- If the this value is undefined, return “[object Undefined]“.
- If the this value is null, return “[object Null]“.
- Let O be the result of calling ToObject passing the this value as the argument.
- Let class be the value of the [[Class]] internal property of O.
- Return the String value that is the result of concatenating the three Strings “[object ", class, and "]“.
由于 JavaScript 中一切都是对象,任何都不例外,对所有值类型应用 Object.prototype.toString.call() 方法结果如下:
console.log(Object.prototype.toString.call(123)) //[object Number]
console.log(Object.prototype.toString.call('123')) //[object String]
console.log(Object.prototype.toString.call(undefined)) //[object Undefined]
console.log(Object.prototype.toString.call(true)) //[object Boolean]
console.log(Object.prototype.toString.call({})) //[object Object]
console.log(Object.prototype.toString.call([])) //[object Array]
console.log(Object.prototype.toString.call(function(){})) //[object Function]
所有类型都会得到不同的字符串,几乎完美。
[1] Object.prototype.toString ()
封装成类:http://blog.csdn.net/mashangyou/article/details/24036233
Object.prototype.toString.call() 区分对象类型(判断对象类型)的更多相关文章
- Object.prototype.toString.call() 区分对象类型
判断一个对象的类型: /** * 判断对象是否为数组 * @param {Object} source 待判断的对象 * @return {Boolean} true|false */ Object. ...
- 关于toString()和valueOf()以及Object.prototype.toString.call()的结合理解
一.先说说String(): String()是全局函数,把对象的值转换为字符串. 语法:String(obj); 任何值(对象)都有String()方法,执行过程是这样的:首先,如果该对象上有toS ...
- typeof操作符,返回数据类型Array.isArray()、Object.prototype.toString.call()
源地址https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/typeof typeof操作符 // N ...
- Object.prototype.toString & typeof
Object.prototype.toString & typeof Object.prototype.toString 获取某个对象属于哪种内置类型 typeof 得到某个对象的类型 差别 ...
- Object.prototype.toString()
Object.prototype.toString()方法返回一个代表该对象的字符串. var o = new Object(); o.toString(); //"[object Obje ...
- Object.prototype.toString.call(obj).slice(8,-1)
1.Object.prototype.toString() 该方法返回描述某个对象数据类型的字符串,如自定义的对象没有被覆盖,则会返回“[object type]”,其中,type则是实际的对象类型. ...
- 使用Object.prototype.toString.call()方法精确判断对象的类型
在JavaScript里使用typeof判断数据类型,只能区分基本类型,即:number.string.undefined.boolean.object. 对于null.array.function. ...
- js中通过Object.prototype.toString方法----精确判断对象的类型
判断是否为函数 function isFunction(it) { return Object.prototype.toString.call(it) === '[object Func ...
- 数据类型的判断 --Object.prototype.toString.call(obj)精准检测对象类型
数据类型的判断 typeof typeof返回一个表示数据类型的字符串,返回结果包括:number.boolean.string.symbol.object.undefined.function等7种 ...
随机推荐
- HTML5图形绘制学习(1)-- Canvas 元素简介
Canvas元素是HTML5中新增的一个专门用来进行图形绘制的元素.和其名称Canvas一样,它就相当于一个画布,我们可以在其上描绘各种图形. 这里所说的绘制图型,不是指我们可以进行可视化的图形绘制, ...
- [makefile] filter-out
$(filter-out ,) 名称:反过滤函数——filter-out.功能:以模式过滤字符串中的单词,去除符合模式的单词.可以有多个模式.返回:返回不符合模式的字串.示例: objects=mai ...
- iOS绘制view
@import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...
- 关于Asp.net超时,延长读取sql server数据库的超时时间!(已解决)
昨天,接到客户反映说应用报“超时时间已到.在操作完成之前超时时间已过或服务器未响应”问题.从网上了一些资料,发现这个问题还是很普遍的.主要有以下两种解决方法: 第一种方法:在web.config中加上 ...
- USB device & USB controller & USB passthrough
目录 USB device USB controller Linux 相关命令 Python 相关库 Libvirt USB passthrough 参考资料 近期往 openstack 里倒腾 US ...
- Unable to execute dex: Multiple dex files define Lorg/ap (
解决这个问题的方法,直接把commons-collections.jar这个jar包删除,一定要删干净啊,各个地方看一下,再clean下,应该没问题了!根据这个英文的目录指示就是Unable to e ...
- Baidu Sitemap Generator插件使用图解教程
这两天因为百度对本博客文章收录更新很慢,一直在网络查找真正的原因和解决方法.最终发现了柳城开发的Baidu Sitemap Generator WordPress插件,最终效果如果还需要验证一段时间. ...
- linux之SQL语句简明教程---CREATE VIEW
视观表 (View) 可以被当作是虚拟表格.它跟表格的不同是,表格中有实际储存资料,而视观表是建立在表格之上的一个架构,它本身并不实际储存资料. 建立一个视观表的语法如下: CREATE VIEW & ...
- 基于最大最小距离的分类数目上限K确定的聚类方法
聚类是数据挖掘很重要的组成部分.而大多数聚类算法都需要事先确定分类数目K.而本文是在实际 情况下确定分类数目K的上限.进而对数据样本进行自动分类. 首先介绍下最大最小距离算法: 设样本集为X{x(1) ...
- PHP内核学习(一)SAPI
学习PHP-src之前,我准备了一份源文件: GitHub下载->https://github.com/helingfeng/php-src 简单分析一下源码的目录结构: 1. root根目录下 ...