判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
来源:互联网 发布:淘宝客服热线 编辑:程序博客网 时间:2024/05/18 12:40
1、typeof(param) 返回param的类型(string)
这种方法是JS中的定义的全局方法,也是编译者们最常用的方法,优点就是使用简单、好记,缺点是不能很好的判断object、null、array、regexp和自定义对象。
示例代码:
var str='str';var arr=['1','2'];var num=1;var bool=true;var obj={name:'test'};var nullObj=null;var undefinedObj=undefined;var reg=/reg/;function fn(){ alert('this is a function');}function User(name){ this.name=name;}var user=new User('user');console.log(typeof(str));console.log(typeof(arr));console.log(typeof(num));console.log(typeof(bool));console.log(typeof(obj));console.log(typeof(nullObj));console.log(typeof(undefinedObj));console.log(typeof(reg));console.log(typeof(fn));console.log(typeof(user));
结果为:
stringobjectnumberbooleanobjectobjectundefinedobjectfunctionobject
2、Object.prototype.toString().call(param) 返回param的类型(string,格式是[object class])
这个方法能支持绝大多数类型的判断,jquery封装的类型判断就用的这个方法。可能有些人看起来有点迷茫,我来给大家分解一下。
1)call(param)函数
a.fun().call(b)的意思在js中是指,让对象b来代替a,然后执行a的fun函数,写个例子:
function Class1() { this.name = "class1"; this.showNam = function() { alert(this.name); } }function Class2() { this.name = "class2"; }var c1 = new Class1(); var c2 = new Class2();c1.showNam.call(c2);运行结果,输出的为class2,而不是class1,这就相当于是方法继承。
所以,Object.prototype.toString().call(param)的意思其实就是,param.prototype.toString(),那么我们为什么不直接写param.prototype.toString(),而是用call()绕一下呢,下面请看2来了解。
2)Object.prototype.toString()
Object是个什么东东呢?,Script56.chm(就是M$官方教程)上说:Obect提供所有 JScript对象通用的功能,其实Object就是所有js对象的祖先,是一个概念,js中的所有对象就是Object的实例,然后不同的对象重写自己独立的方法。而prototype,大家就没必要追究太深了,它就是返回一个原型的引用,然可以可以动态的给原型添加方法和属性
一个小例子
function class(){ this.name = "class"; this.showName = function(){ alert(this.name); }}var obj = new class();obj.showName();class.prototype.showNameContact = function(){ alert("prototype test"+this.name);}obj.showNameContact();那么就会分别输出 class和prototype test class,本来构造函数class() 里是没有定义showNameContact函数的,而通过prototype我们就可以给对象原型动态添加函数,new的示例中自然就会有了。所以Object.prototype.toString()的意思就是执行Object这个祖先中的toString方法。
那么toString()是干嘛的呢?很多js手册中对toString()函数是这样定义的:
toString() 方法可把一个逻辑值转换为字符串,并返回结果,语法为:booleanObject.toString()。刚才我说了,js中的对象都是继承的Object,这些对象都自定义的有函数或者重构了Object的部分函数,而且它们都对toString()函数进行了重写。所以我们不能想1中直接写param.prototype.toString()这样就执行的是param自己重写后的toString()函数了。
好了,到关键的时刻了,toString()到底是干嘛的呢,有什么作用呢?
在ES3中,Object.prototype.toString方法的规范如下:
Object.prototype.toString()
在toString方法被调用时,会执行下面的操作步骤:
1. 获取this对象的[[Class]]属性的值.
2. 计算出三个字符串"[object ", 第一步的操作结果Result(1), 以及 "]"连接后的新字符串.
3. 返回第二步的操作结果Result(2).
在ES3中,规范文档并没有总结出[[class]]内部属性一共有几种,不过我们可以自己统计一下,原生对象的[[class]]内部属性的值一共有10种.分别是:"Array", "Boolean", "Date", "Error", "Function", "Math", "Number", "Object","RegExp", "String".所以Object.prototype.toString()的输出结果就是这种格式的字符串[object Array],[object Boolean]。
在ES5.1中,除了规范写的更详细一些以外,Object.prototype.toString方法和[[class]]内部属性的定义上也有一些变化,Object.prototype.toString方法的规范如下:
Object.prototype.toString ( )
在toString方法被调用时,会执行下面的操作步骤:
1 如果this的值为undefined,则返回"[object Undefined]".
2 如果this的值为null,则返回"[object Null]".
3 让O成为调用ToObject(this)的结果.
4 让class成为O的内部属性[[Class]]的值.
5 返回三个字符串"[object ", class, 以及 "]"连接后的新字符串.
可以看出,比ES3多了1,2,3步.第1,2步属于新规则,比较特殊,因为"Undefined"和"Null"并不属于[[class]]属性的值。经统计,可返回的类型有"Arguments", "Array", "Boolean", "Date", "Error", "Function", "JSON", "Math", "Number", "Object", "RegExp", "String"比ES3多了2种分别是arguments对象的[[class]]成了"Arguments",而不是以前的"Object",还有就是多个了全局对象JSON,它的[[class]]值为"JSON"。
最后的最后提醒大家,Object.prototype.toString().call(param)返回的[object class]中class首字母是大写,像JSON这种甚至都是大写,所以,大家判断的时候可以都转换成小写,以防出错,Object.prototype.toString().call(param).toLowerCase()即可。
转载自:http://www.jb51.net/article/42864.htm
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- js判断对象类型 typeof与Object.prototype.toString.call方法的区别
- js中精确判断对象类型--关于typeof 和Object.prototype.toString方法的区别
- 判断变量的类型(typeof、instanceof、Object.prototype.toString)
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中通过Object.prototype.toString方法----精确判断对象的类型
- js中通过Object.prototype.toString方法----精判断对象的类型
- js中判断数据类型的属性typeof、instanceof、 constructor、 prototype
- 判断javascrit 类型 Object.prototype.toString方法的原理
- JS类型判断typeof PK {}.toString.call(obj);
- 判断js中的类型:typeof / instanceof / constructor / prototype
- js中的类型判断typeof、instanceof、constructor和prototype
- js中判断一个对象的类型的方法typeof/constructor/instanceof
- js用typeof方法判断undefined类型
- js中判断变量类型函数typeof的用法总结
- js中typeof只能判断基础类型。
- js进行类型判断 Object.prototype.toString.call()
- hiveserver问题
- 简单理解java反射机制
- 杂记<二>
- MySQL5.6分区数量太多引发的血案
- 2016年第一周目标制定
- 判断js中各种数据的类型方法之typeof与0bject.prototype.toString讲解
- ajax 跨域访问问题
- static
- mybatis番外篇(一)自动生成键selectkey
- urllib 详解
- leetcode1--Two Sum
- HashMap、HashTable、LinkedHashMap和TreeMap
- linux的nohup命令的用法
- 五家共井