原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html

如何判断js中的数据类型:typeof、instanceof、 constructor、 prototype方法比较

如何判断js中的类型呢,先举几个例子:

var a = "iamstring.";

var b = 222;

var c= [1,2,3];

var d = new Date();

var e = function(){alert(111);};

var f = function(){this.name="22";};

最常见的判断方法:typeof

alert(typeof a)   ------------> string

alert(typeof b)   ------------> number

alert(typeof c)   ------------> object

alert(typeof d)   ------------> object

alert(typeof e)   ------------> function

alert(typeof f)   ------------> function

其中typeof返回的类型都是字符串形式,需注意,例如:

alert(typeof a == "string") -------------> true

alert(typeof a == String) ---------------> false

另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。

判断已知对象类型的方法: instanceof

alert(c instanceof Array) ---------------> true

alert(d instanceof Date)

alert(f instanceof Function) ------------> true

alert(f instanceof function) ------------> false

注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

根据对象的constructor判断: constructor

alert(c.constructor === Array) ----------> true

alert(d.constructor === Date) -----------> true

alert(e.constructor === Function) -------> true

注意: constructor 在类继承时会出错

eg,

function A(){};

function B(){};

A.prototype = new B(); //A继承自B

var aObj = new A();

alert(aobj.constructor === B) -----------> true;

alert(aobj.constructor === A) -----------> false;

而instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true:

alert(aobj instanceof B) ----------------> true;

alert(aobj instanceof B) ----------------> true;

言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:

aobj.constructor = A; //将自己的类赋值给对象的constructor属性

alert(aobj.constructor === A) -----------> true;

alert(aobj.constructor === B) -----------> false; //基类不会报true了;

通用但很繁琐的方法: prototype

alert(Object.prototype.toString.call(a) === ‘[object String]’) -------> true;

alert(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true;

alert(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true;

alert(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true;

alert(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true;

alert(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;

大小写不能写错,比较麻烦,但胜在通用。

通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,简单总结下,挖个坑,欢迎补充!

[转]如何判断js中的数据类型的更多相关文章

  1. 如何判断js中的数据类型?

    js六大数据类型:number.string.object.Boolean.null.undefined string: 由单引号或双引号来说明,如"string" number: ...

  2. 如何判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  3. 如何判断js中的数据类型(转)

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  4. 判断js中的数据类型

    如何判断js中的数据类型:typeof.instanceof. constructor. prototype方法比较 如何判断js中的类型呢,先举几个例子: var a = "iamstri ...

  5. 判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  6. 转:判断js中的数据类型的几种方法

    判断js中的数据类型有一下几种方法:typeof.instanceof. constructor. prototype. $.type()/jquery.type(),接下来主要比较一下这几种方法的异 ...

  7. js中获取数据类型

    ES5中,js中数据类型:number.string.boolean.undefined.null.object js中获取数据类型常用的四种方式 实例: var a = 123, b = true, ...

  8. js中的数据类型和判断数据类型

    js中的数据类型和判断数据类型 基本数据类型,六大基本数据类型:字符串(String).数字(Number).布尔(Boolean).对象(Object).空(Null).未定义(Undefined) ...

  9. js中的数据类型

    JS中的数据类型: ——数字  (number)NaN ——字符串(string) ——布尔  (boolean)——函数  (function)     也是对象的一种 ——对象  (object) ...

随机推荐

  1. (七)中介者模式-C++实现

    用一个中介对象来封装一系列的对象交互.中介者使各对象不需要显示地相互引用,从而使其解耦合松散而且可以独立地改变他们之间的交互. 中介者模式适合于 系统中不希望对象之间直接交互,即不希望类之间相互包含, ...

  2. AngularJs定制样式插入到ueditor中的问题总结

    总结一下自己给编辑器定制样式的过程中所遇到的问题,主要是编辑器的二次开发接口,以及用angular定制样式,问题不少,终于在**的帮助下,完成了,还剩下老版本和新版本的交互没有弄好,不过不难.下面分别 ...

  3. 05章 OGNL

    一.OGNL全称是Object Graph Navigation Language,即对象导航图语言 OGNL在框架中主要做两件事情:表达式语言和类型转换器 OGNL在框架中的作用以及数据的流入流出: ...

  4. java的多线程总结

    在java中要想实现多线程,有两种手段,一种是继续Thread类,另外一种是实现Runable接口. 对于直接继承Thread的类来说,代码大致框架是: class 类名 extends Thread ...

  5. NOIP模拟赛 隔壁

    问题描述: 隔壁学校地形图可以通过一个高度矩阵表示,矩阵中每一个位置都有一个数0<=hij<=10^5表示这个坐标的海拔,我们姑且将其称为海拔图,容易发现,我们可以通过这个矩阵轻松算出隔壁 ...

  6. three.js 之旅 (三)

    复制自:http://www.cnblogs.com/ssrsblogs/p/5611332.html 创建模型: 1.长方体: THREE.CubeGeometry(width, height, d ...

  7. 033医疗项目-模块三:药品供应商目录模块——供货商药品目录t添加查询功能----------Dao层和Service层和Action层和调试

    什么叫做供货商药品目录t添加查询功能?就是说我们前面的博客里面不是说供货商登录后看到了自己供应的药品了么如下: 现在供货商想要往里面添加别的药品,那么这个药品的来源就是卫生局提供的那个Ypxx表(药品 ...

  8. 解决ssh-connect-to-host-github-com-port-22-connection-timed-out

    PC:~$ ssh git@github.com ssh: connect to host github.com port 22: Connection timed out 解决办法:(linux下) ...

  9. Linux 进程详解

    Linux内核的七大区间 .进程管理(进程创建,进程的三种状态,进程间的调度,调度算法...) .内存管理(段式管理(Linux所有段都从0开始),页式管理--地址偏移量) .系统调用(C语言库函数的 ...

  10. H2 Database入门

    H2 Database做为轻量级的内嵌数据库,功能十分强大,而且运行时只需要一个jar包即可,下表是官网的描述: 更详细的对比见官网页面: http://www.h2database.com/html ...