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种 ...
随机推荐
- thinkphp微信开发:安全模式消息加解密
使用thinkphp官方的WeChat包,使用不同模式可以成功,但是安全模式就是不行,现将分析解决结果做下记录. TRight 分析问题: 解密微信服务器消息老是不成功,下载下微信公众平台官方给出的解 ...
- URI、URL和URN之间的区别与联系
URI:Uniform Resource Identifier,统一资源标识符: URL:Uniform Resource Locator,统一资源定位符: URN:Uniform Resource ...
- Leetcode 283 Move Zeroes python
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- Nand flash 的发展和eMMC
讨论到eMMC的发展历程,必须要从介绍Flash的历史开始 Flash分为两种规格:NOR Flash和NAND Flash,两者均为非易失性闪存模块. 1988年,Intel首次发出NOR flas ...
- Visual Studio 2015 Update 3 RC 候选预览版粗来了
.Net 基金会 http://www.dotnetfoundation.org/ 更新的真快,刚打完2的补丁包,3就粗来了............ https://www.visualstudio. ...
- 【Javascript下载文件的Post实现】
/** *从服务器上下载数据 *@param paras Json格式的键值对参数 */ downLoadFromServer: function (paras) { //init a new win ...
- Blacksmith test
最近使用Blacksmith 对各种K,V数据库做了一些测试,从中了解了一些各种数据库的设计方式,比较各种数据库的性能 BlaskSmith是我们自己的产品,详细的产品信息可以在github上看到 h ...
- POJ 2892 Tunnel Warfare (SBT + stack)
题意:给定了初始的状态:有n个村庄连成一条直线,现在有三种操作: 1.摧毁一个村庄 2.询问某个村庄,输出与该村庄相连的村庄数量(包括自己) 3.修复被摧毁的村庄,优先修复最近被摧毁的........ ...
- JTextPane 的 undo 、 redo
实现文本框输入内容的单条记录撤销,重做,通过按钮实现 以及通过JList的多条撤销.重做操作(类似PS) 昨天还在为自己写不出代码怎么办而伤心,没想到今天上午就实现了,并且还完善了功能: 可以在撤销一 ...
- sql 2000 "无法执行查询,因为一些文件缺少或未注册"的
sql 2000 "无法执行查询,因为一些文件缺少或未注册"的解决办法 在SQL server 2000中打开表查看数据的时候,提示说“无法执行查询,因为一些文件缺少或未注册” 用 ...