如何判断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方法,简单总结下,挖个坑,欢迎补充!

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

如何判断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中的数据类型

    原文地址:http://blog.sina.com.cn/s/blog_51048da70101grz6.html 如何判断js中的数据类型:typeof.instanceof. constructo ...

  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. 批量生成sqlldr文件,高速卸载数据

    SQL*Loader 是用于将外部数据进行批量高速加载的数据库的最高效工具,可用于将多种平面格式文件加载到Oracle数据库.SQL*Loader支持传统路径模式以及直接路径这两种加载模式.关于SQL ...

  2. 基于CentOS与VmwareStation10搭建Oracle11G RAC 64集群环境:2.搭建环境-2.4. 安装JDK

    2.4.安装JDK 2.4.1.准备JDK 在百度搜索:JDK下载 2.4.2.上传JDK put E:\软件安装文件\jdk-8u11-linux-x64.rpm /home/linuxrac1/D ...

  3. JSONP实例

    JSONP实例 package sus.app; import java.io.IOException; import java.util.Map; import javax.servlet.http ...

  4. winform 防止多開

    場景: 當我們的電腦可以使用多用戶同時登錄時候,每個使用者只允許執行一次exe程式. 例如:一台公用電腦,有多個用戶A.B. 當用戶A進入系統第一次運行C:\XX.exe,OK.第二次運行XX.exe ...

  5. Android中垃圾回收日志信息

    原因 GC_CONCURRENTfreed 178K, 41% free 3673K/6151K, external 0K/0K, paused 2ms+2msGC_EXPLICITfreed 6K, ...

  6. delphi 中字符串与16进制、10进制转换函数

      //字符串转成16进制代码function strToHexStr(str:string):string;varc:char;ss:string;i:integer;beginwhile str& ...

  7. [转]linux 下使用dump和restore命令

    转自:http://blog.sina.com.cn/s/blog_63eb479a01011sdu.html dump 支持分卷和增量备份(所谓增量备份是指备份最近一次备份以来修改过的文件,也称差异 ...

  8. Spring MVC 问题列表:目录

    学习SpringMVC时遇到不少问题,这里将其汇总. 1.怎么搭建SpringMVC 2.SpringMVC和Spring使用是配置到一个文件中还是两个配置文件 3.SpringMVC接受从前台请求 ...

  9. Linux系统下查看已经登录用户并踢出的方法

    LINUX是个多用户系统,一旦连接到网络中,它可以同时为多个登录用户提供服务. 查看用户的操作 查看当前用户: [ROOT@LOCALHOST ROOT] # W                    ...

  10. Xcode集成Google Test

    Xcode集成Google Test 1.下载源代码https://github.com/google/googletest 2.进入下载文件目录下googletest/make中运行make gte ...