当前位置: 首页 > news >正文

判断js数据类型

1.typeof

2.constructor

3.instanceof

4.prototype

5.$.type()


1、最常见的判断方法: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类型的对象时比较方便。

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

alert(c instanceof Array) ---------------> true
alert(d instanceof Date) alert(f instanceof Function) ------------> true alert(f instanceof function) ------------> false 注意:instanceof 后面一定要是对象类型,并且大小写不能错,该方法适合一些条件选择或分支。

3、根据对象的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了;

4、通用但很繁琐的方法: 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; 大小写不能写错,比较麻烦,但胜在通用。

5、无敌万能的方法:jquery.type()

如果对象是undefined或null,则返回相应的“undefined”或“null”。
jQuery.type( undefined ) === "undefined"
jQuery.type() === "undefined"
jQuery.type( window.notDefined ) === "undefined" jQuery.type( null ) === "null" 如果对象有一个内部的[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,我们返回相应的 [[Class]] 名字。 (有关此技术的更多细节。 ) jQuery.type( true ) === "boolean" jQuery.type( 3 ) === "number" jQuery.type( "test" ) === "string" jQuery.type( function(){} ) === "function" jQuery.type( [] ) === "array" jQuery.type( new Date() ) === "date" jQuery.type( new Error() ) === "error" // as of jQuery 1.9 jQuery.type( /test/ ) === "regexp" 其他一切都将返回它的类型“object”。

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

 

转载于:https://www.cnblogs.com/Jane77/p/7504918.html

相关文章:

  • Linux上给不是管理员的用户增加安装软件的权限
  • 【已解决】项目加载失败,Web应用程序项目XX已配置为使用IIS
  • JDBC连接数据库:单线程、多线程、批处理插入数据的对比
  • VS2015 +EF6 连接MYSQL数据库生成实体
  • CF 840 D
  • 初识oracle存储过程
  • 大数据竞赛平台Kaggle案例实战
  • 我的Hibernate学习记录(一)
  • Java 读写Properties配置文件
  • 输出斐波那契数列前20项,每输出5个数换行
  • MySQL5.6安装步骤
  • 【转载】max/min函数的用法
  • Linux-Ubuntu下配置telnet环境
  • linux下git常用命令
  • 通过命令行操作MYSQL的方法 以及导入大的SQL备份文件
  • 【5+】跨webview多页面 触发事件(二)
  • Android单元测试 - 几个重要问题
  • Java 11 发布计划来了,已确定 3个 新特性!!
  • JS+CSS实现数字滚动
  • Redash本地开发环境搭建
  • Spring Cloud Feign的两种使用姿势
  • Vue组件定义
  • 第十八天-企业应用架构模式-基本模式
  • 关于Flux,Vuex,Redux的思考
  • 基于组件的设计工作流与界面抽象
  • 巧用 TypeScript (一)
  • 如何优雅地使用 Sublime Text
  • 什么是Javascript函数节流?
  • 使用 Xcode 的 Target 区分开发和生产环境
  • 提升用户体验的利器——使用Vue-Occupy实现占位效果
  • 微信公众号开发小记——5.python微信红包
  • 用jquery写贪吃蛇
  • 做一名精致的JavaScripter 01:JavaScript简介
  • ​io --- 处理流的核心工具​
  • ​sqlite3 --- SQLite 数据库 DB-API 2.0 接口模块​
  • ​马来语翻译中文去哪比较好?
  • ​软考-高级-信息系统项目管理师教程 第四版【第19章-配置与变更管理-思维导图】​
  • #android不同版本废弃api,新api。
  • #mysql 8.0 踩坑日记
  • (1)(1.13) SiK无线电高级配置(六)
  • (1)Map集合 (2)异常机制 (3)File类 (4)I/O流
  • (20)目标检测算法之YOLOv5计算预选框、详解anchor计算
  • (MATLAB)第五章-矩阵运算
  • (八)光盘的挂载与解挂、挂载CentOS镜像、rpm安装软件详细学习笔记
  • (超详细)语音信号处理之特征提取
  • (附源码)apringboot计算机专业大学生就业指南 毕业设计061355
  • (解决办法)ASP.NET导出Excel,打开时提示“您尝试打开文件'XXX.xls'的格式与文件扩展名指定文件不一致
  • (完整代码)R语言中利用SVM-RFE机器学习算法筛选关键因子
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转载)微软数据挖掘算法:Microsoft 时序算法(5)
  • .net FrameWork简介,数组,枚举
  • .NET 回调、接口回调、 委托
  • .NET 使用配置文件
  • .NET 中各种混淆(Obfuscation)的含义、原理、实际效果和不同级别的差异(使用 SmartAssembly)
  • .NET/MSBuild 中的发布路径在哪里呢?如何在扩展编译的时候修改发布路径中的文件呢?