Object.prototype.toString.call 判断一个对象的类型
来源:互联网 发布:学java月薪多少 编辑:程序博客网 时间:2024/05/18 16:38
转自:http://www.veryhuo.com/a/view/52778.html
在 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) //trueconsole.log(b instanceof Object) //trueconsole.log(c instanceof Object) //true//只有 Array 类型的 b 才是 Array 的实例console.log(a instanceof Array) //falseconsole.log(b instanceof Array) //trueconsole.log(c instanceof Array) //false//只有 Function 类型的 c 才是 Function 的实例console.log(a instanceof Function) //falseconsole.log(b instanceof Function) //falseconsole.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]
所有类型都会得到不同的字符串,几乎完美。
- Object.prototype.toString.call 判断一个对象的类型
- Object.prototype.toString.call()进行类型判断
- Object.prototype.toString.call() 进行类型判断
- Object.prototype.toString.call() 区分对象类型
- Object.prototype.toString.call() 区分对象类型
- Object.prototype.toString.call() 区分对象类型
- Object.prototype.toString.call() 区分对象类型
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- js进行类型判断 Object.prototype.toString.call()
- typeof、instanceof、Object.prototype.toString.call() 区分对象类型
- Object.prototype.toString.call(obj)检测对象类型?
- Object.prototype.toString.call 与数组判断
- Object.prototype.toString.call 与数组判断
- Object.prototype.toString.call
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中精确判断对象类型--关于typeof 和Object.prototype.toString方法的区别
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中通过Object.prototype.toString方法----精确判断对象的类型
- BoundsChecker下载
- List、Map遍历过程中删除某些项
- 计算几何【转】
- MFC ActiveX (OCX) 创建技术
- NSBlog
- Object.prototype.toString.call 判断一个对象的类型
- Struts2拦截器的简单示例
- hdu 题目 (最小生成树 prim算法)
- 为开发者准备的15 款Tooltip工具提示jQuery插件
- 如何在oracle中查询所有用户表的表名、主键名称、索引、外键等
- Customize NSLog for Easier Debugging
- HQL: Hibernate查询语言
- 20 个 jQuery 和 CSS 的文本特效插件
- 开启设计模式之旅(一)