1、最常见的判断方法:typeof
  1. console.log(typeof a) ------------> string
  2. console.log(typeof b) ------------> number
  3. console.log(typeof c) ------------> object
  4. console.log(typeof d) ------------> object
  5. console.log(typeof e) ------------> function
  6. console.log(typeof f) ------------> function
  7. 其中typeof返回的类型都是字符串形式,需注意,例如:
  8. console.log(typeof a == "string") -------------> true
  9. console.log(typeof a == String) ---------------> false
  10. 另外typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。
2、判断已知对象类型的方法: instanceof
  1. alert(c instanceof Array) ---------------> true
  2. alert(d instanceof Date)
  3. alert(f instanceof Function) ------------> true
  4. alert(f instanceof function) ------------> false
  5. 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。
3、根据对象的constructor判断: constructor
  1. alert(c.constructor === Array) ----------> true
  2. alert(d.constructor === Date) -----------> true
  3. alert(e.constructor === Function) -------> true
  4. 注意: constructor 在类继承时会出错
  5. eg
  6. function A(){};
  7. function B(){};
  8. A.prototype = new B(); //A继承自B
  9. var aObj = new A();
  10. alert(aobj.constructor === B) -----------> true;
  11. alert(aobj.constructor === A) -----------> false;
  12. instanceof方法不会出现该问题,对象直接继承和间接继承的都会报true
  13. alert(aobj instanceof B) ----------------> true;
  14. alert(aobj instanceof B) ----------------> true;
  15. 言归正传,解决construtor的问题通常是让对象的constructor手动指向自己:
  16. aobj.constructor = A; //将自己的类赋值给对象的constructor属性
  17. alert(aobj.constructor === A) -----------> true;
  18. alert(aobj.constructor === B) -----------> false; //基类不会报true了;
4、通用但很繁琐的方法: prototype
  1. alert(Object.prototype.toString.call(a) === ‘[object String]’) -------> true;
  2. alert(Object.prototype.toString.call(b) === ‘[object Number]’) -------> true;
  3. alert(Object.prototype.toString.call(c) === ‘[object Array]’) -------> true;
  4. alert(Object.prototype.toString.call(d) === ‘[object Date]’) -------> true;
  5. alert(Object.prototype.toString.call(e) === ‘[object Function]’) -------> true;
  6. alert(Object.prototype.toString.call(f) === ‘[object Function]’) -------> true;
  7. 大小写不能写错,比较麻烦,但胜在通用。
5、无敌万能的方法:jquery.type()
  1. 如果对象是undefinednull,则返回相应的“undefined”或“null”。
  2. jQuery.type( undefined ) === "undefined"
  3. jQuery.type() === "undefined"
  4. jQuery.type( window.notDefined ) === "undefined"
  5. jQuery.type( null ) === "null"
  6. 如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 )
  7. jQuery.type( true ) === "boolean"
  8. jQuery.type( ) === "number"
  9. jQuery.type( "test" ) === "string"
  10. jQuery.type( function(){} ) === "function"
  11. jQuery.type( [] ) === "array"
  12. jQuery.type( new Date() ) === "date"
  13. jQuery.type( new Error() ) === "error" // as of jQuery 1.9
  14. jQuery.type( /test/ ) === "regexp"

其他一切都将返回它的类型“object”。

通常情况下用typeof 判断就可以了,遇到预知Object类型的情况可以选用instanceof或constructor方法,实在没辙就使用$.type()方法。

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

  1. js中判断数据类型的四种方法总结

    js中判断数据类型的四种方法 前言 在js中,我们经常需要判断数据的类型,那么哪些方法可以用来判断数据的类型呢?哪种方法判断数据类型最准确呢? 我们来一个个分析: 1.typeof typeof是一个 ...

  2. JS中判断数据类型的几种方法

    1⃣️首先我们来了解一下js中的数据类型 1.基本数据类型:Undefined.Null.Boolean.Number.String(值类型) 2.复杂数据类型:Object(引用类型) (值类型和引 ...

  3. js中判断数据类型的4中方法

    注意: js中数据类型有7种(number, boolean, string, null, undefined, object, Symbol(es6新增)) 原始数据类型: number, stri ...

  4. JS 中 判断数据类型 typeof详解

    typeof 可用来获取检测变量的数据类型 语法 typeof operand typeof(operand) 参数 operand   一个表示对象或原始值的表达式,其类型将被返回. 描述 下表总结 ...

  5. JS 中判断数据类型是否为 null、undefined 或 NaN

    判断 undefined var aaa = undefined; console.log(typeof(aaa) === "undefined"); // true 判断 nul ...

  6. js中判断数据类型

    一般来说,可以使用typeof来判断数据类型,但是数组,对象和null的结果都是object,那么如何区分这三类呢?可以使用如下方法: var arr = []; var obj = {} var e ...

  7. js中判断数据类型的方法 typeof

    <input type="text" onblur="demo(this)"/><br/> <input type="n ...

  8. js精确判断数据类型为何用Object.prototype.toString.call()而不是Object.prototype.toString()

    有何区别,为何一定要通过call. 我们知道call是用来改变函数作用域的,Object.prototype.toString.call在这儿也是用来改变作用域的. Object.prototype. ...

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

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

随机推荐

  1. (三)PHP网页架站

    目前,Windows下已经有集成的PHP网页架站工具,例如:AppServ.WampServer.这些软件将Apache.PHP.MySQL.phpMyAdmin集成到一起,极大地方便了开发者架站.但 ...

  2. 服务器LIUNX之如何解决矿机问题

    点进来的基本都是遇到liunx变矿机的小伙伴吧(cpu运载300%) 卡的连终端都很难打开 开下来之后提示 大意是, 到xxx网站给钱了事, 不过基本这个网站基本也上不去, 要么是暴力破解, 要么是通 ...

  3. [转]Tomcat优化之内存、并发、缓存

    1.Tomcat内存优化 Tomcat内存优化主要是对 tomcat 启动参数优化,我们可以在 tomcat 的启动脚本 catalina.sh 中设置 JAVA_OPTS 参数. JAVA_OPTS ...

  4. TiDB, Distributed Database

    https://www.zhihu.com/topic/20062171/top-answers

  5. 16_Queue_利用wait()和notify()编写一个阻塞队列

    [线程间通信概念] 线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体,线程间的通信就成为整体必用方式之一.当线程存在通信指挥,线程间的交互性会更强大,在提高CPU利用率的同 ...

  6. CAA介绍(转)

    CAA是DS公司正对于其一系列产品,eg:CATIA,ENOVIA,DELIMA,etc,进行二次开发的一个环境.与VC结合的比较紧密.CAAV4是用于Unix/Linux的,到CAAV5才移植到Wi ...

  7. 【Leetcode】【Easy】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3, ...

  8. 319. Bulb Switcher (Math, Pattern)

    There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every ...

  9. IOS MapKit框架的使用(专门用于地图显示)

    ● MapKit框架使用前提 ● 导入框架 ● 导入主头文件#import <MapKit/MapKit.h>   ●  MapKit框架使用须知 ●  MapKit框架中所有数据类型的前 ...

  10. Codeforces Round #460 (Div. 2)

    A. Supermarket We often go to supermarkets to buy some fruits or vegetables, and on the tag there pr ...