JavaScript判断数据类型的方式
JavaScript判断数据类型的方式
js中的数据类型有哪些?
- 基本数据类型:number、string、boolean、null、undefined、symbol以及未来ES10新增的BigInt(任意精度调整)。
- 引用数据类型:对象Object、数组Array、函数Function
判断数据类型的方法有哪些?
- 最常见的判断方法:typeof
- 已知对象类型:instanceof
- 对象原型链判断方法:Object.prototype.toString.call()
- 根据对象的构造器constructor进行判断
- jQuery方法:jQuery.type()
- 严格运算符:===
一、typeof
注:typeof返回的类型都是字符串形式。
返回六个字符串:string, boolean, number, function, object, undefined
typeof null // object
typeof function(){} // function
typeof new Function() // function
// 基本数据类型判断
typeof 1 // 'number'
typeof 'a' // 'string'
typeof false // 'boolean'
typeof undefined // 'undefined'
typeof Symbol() // 'symbol'// 引用数据类型判断和new关键字创建的变量判断类型
typeof {} // 'object'
typeof [] // 'object'
typeof new Number(1) // 'object'
typeof new String('a') // 'object'
typeof new Boolean(true) // 'object'
typeof new Number(1) // 'object'
typeof new Date() // 'object'
function Car () {} // 定义一个构造函数
typeof new Car() // 'object'
我们可以发现,typeof在判断null、array、object以及函数实例(new + 函数)时,得到的都是object,这使得在判断这些数据类型的时候,得不到真正的结果,由此引出instanceof。
二、instanceof
注:instanceof后面一定要是对象类型,并且大小写不能写错。
function Car () {} // 定义一个构造函数console.log(new Car() instanceof Car) // trueconsole.log(new Car() instanceof Function) // falseconsole.log(new Car() instanceof Object) // trueconsole.log({} instanceof Object) // trueconsole.log([] instanceof Array) // trueconsole.log(new String('') instanceof String) // trueconsole.log('' instanceof String) // false
三、对象原型链判断方法:Object.prototype.toString.call()
Object.prototype.toString.call(null) // [object Null]
Object.prototype.toString.call(undefined) // [object Undefined]
Object.prototype.toString.call(1) // [object Number]
Object.prototype.toString.call(new Number(1)) // [object Number]
Object.prototype.toString.call('a') // [object String]
Object.prototype.toString.call(new String('a')) // [object String]Object.prototype.toString.call({}) // [object Object]
Object.prototype.toString.call([]) // [object Array]
Object.prototype.toString.call(new Date()) // [object Date]
Object.prototype.toString.call(/^d$/g) // [object RegExp]
Object.prototype.toString.call(() => {}) // [object Function]
Object.prototype.toString.call(new Function()) // [object Function]function Car () {} // 定义一个构造函数
Object.prototype.toString.call(new Car()) // [object Object]
toString方法,在Object原型上返回数据格式。
四、根据对象的constructor进行判断
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";};
c.constructor === Array // true
d.constructor === Date // true
e.constructor === Function // true
constructor判断方法跟instanceof相似,但是constructor检测Object与instanceof不一样,constructor还可以处理基本数据类型的检测,不仅仅是对象类型。
注:null和undefined没有constructor
判断数字时使用(),例如:(123).constructor
constructor在类继承时会出错,因为Object被覆盖掉了,检测结果会出错。
五、jQuery.type()
如果对象是null或undefined,则直接返回“null”和“undefined”。
注:在使用时,一定要引入jQuery文件,不然会报错,jQuery is not defined
console.log(jQuery.type(undefined) === "undefined") // true
console.log(jQuery.type() === "undefined") // true
console.log(jQuery.type(window.notDefined) === "undefined") // true
console.log(jQuery.type(152) === "number") // true
console.log(jQuery.type('188') === "string") // true
console.log(jQuery.type([]) === "array") // true
console.log(jQuery.type(true) === "boolean") // true
console.log(jQuery.type(function(){}) === "function") // true
console.log(jQuery.type(new Date()) === "date") // true
console.log(jQuery.type(/\d/) === "regexp") // true
console.log(jQuery.type(new Error()) === "error") // true jquery版本高于1.9.3
console.log(jQuery.type({name:'Hello'}) === "object") // true
console.log(jQuery.type(Symbol()) === "symbol") // true
六、严格运算符:===
通常用于判断一个变量是否为空,变量是否为数据等。
var a = null;
typeof a; // 'object'
a === null; // true
JavaScript判断数据类型的方式相关推荐
- JavaScript 判断数据类型
JavaScript 判断数据类型 首先JavaScript基本数据类型有:number null undefined string boolean es6以后还新增了bigint和symbol (上 ...
- JavaScript判断数据类型的方法
JavaScript判断数据类型的方法 1 数据类型有哪些? 2 判断JavaScript数据类型的方法 2.1 typeof 2.2 instanceof 2.3 constructor 2.4 t ...
- JavaScript判断数据类型是不是数组
JavaScript判断数据类型是不是数组 1.Array.isArray(es6 新增) 在这里插入代码片 Array.isArray([]) true Array.isArray({}) fals ...
- JavaScript 判断数据类型的方法
文章目录 1.javascript 中的数据类型 2.`typeof` 返回 变量的数据类型 3.`instanceof ` :复杂数据类型的判断 3.1.instanceof 原理分析(初学者跳过) ...
- JavaScript学习总结(六)——JavaScript判断数据类型总结
最近做项目中遇到了一些关于javascript数据类型的判断处理,上网找了一下资料,并且亲自验证了各种数据类型的判断,在此做一个总结吧! 一.JS中的数据类型 1.数值型(Number):包括整数.浮 ...
- javascript 判断数据类型的几种方法 1
1.typeof 类型判断 缺点:无法区分null .对象.数组,Map,Set,WeakMap,WeakSet.RegExp等 注意:通过构造函数创建的变量typeof 后是都是object var ...
- JavaScript判断数据类型有几种方法,以及它们的区别
JavaScript有五种数据判断类型方法: typeof instanceof constructor Object.prototype.toString.call()
- JavaScript判断数据类型
1.定义检测数据 const num = 123; const str = 'aaa'; const bool = true; const arr = [1, 2]; const json = {na ...
- javaScript中判断数据类型的方法
目录 一.javaScript数据类型 二.javaScript判断数据类型的方法 1.使用typeof 2.使用instanceof 3.使用Object.prototype.toString.ca ...
最新文章
- 微软私有云系列----域服务器准备
- 提升深度学习模型性能及网络调参
- Php小数转为百分数,学习猿地-php百分数如何转小数
- python回溯算法_什么是回溯法,Python解法交流?
- java获取服务器信息吗_java获取服务器一些信息的方法
- 《微机原理及接口技术》第08章在线测试
- Java中的Atomic包
- k8s dashboard_【大强哥-k8s从入门到放弃02】Kubernetes1.17部署Dashboard2.0
- Java 进栈出栈的过程
- serversocket中的backlog是什么_输入网址按回车,到底发生了什么
- 在raspbian上配置apache2/subversion/xdebug及mysql远程访问
- emqx http not found 怎么回事_幽默笑话:行倒是行,但两个大男人这么说话算怎么回事...
- php调用纯真ip,PHP调用纯真IP数据库返回具体地址
- fedora mysql添加密码_Fedora14下 mysql更改密码
- 经济学外文文献在哪查?
- 尤雨溪对 2022 Web前端生态趋势是这样看的
- 前端实时可视化开发工具的使用
- 使用Python绘制词云图片
- SRS 代码分析【mpeg-ts解析】
- 个人阅读作业+个人总结
热门文章
- 第八部分 验证码的识别(极验验证码)
- 依图医疗发布AI全部位辅助诊断产品 全球癌症筛查智能诊疗平台助力肿瘤早筛...
- Codeforces Round #842 (Div. 2) 个人题解
- 游龙传说服务器维护,炉石传说服务器维护故障补偿方案详细内容
- js,e.pageX、pageY模态框拖动
- 虚惊一场?英特尔为几乎所有现代 CPU 发布神秘补丁
- ZBrush中如何导出模型和贴图
- 假设有一段英文,其中有的字母I误写为i,请编写程序进行纠正。
- Accessibility辅助功能--一念天堂,一念地狱
- 【密码效验最强正则表达式】