JS 判断数据类型

数据类型

1、数据类型:

  • 值类型(基本数据类型):字符串(string)、数字(Number)、布尔(Boolean)、对空(Null)、未定义(undefined)、Symbol(es6引入,独一无二的)
  • 引用数据类型:对象( Object)、数组(Array)、函数(Function)。

2、判断类型的方法

  1. typeof();
    主要用于检测基本数据类型,判断引用类型时只能区分 object,undefined 和 function
	typeof "5"  // string
    typeof 5    // number
    typeof true // boolean
    typeof Symbol("5") // symbol
    typeof 5n   // bigint
    typeof null // object
    typeof new Object(); // object
    typeof new Function(); //function
    typeof([1,2]) // object
  1. instanceof()
    主要用于引用数据类型的检测,判断同一个作用域下,一个对象/类是否是另一个对象/类的实例。会一直递归到最终原型。基本数据类型没有父类型,都会返回false
    原理:用 A instanceof B 可以判断 A 是不是 B 的实例,返回一个布尔值,由构造类型判断出数据类型
	console.log( arr instanceof Array );  	// true
	console.log( date instanceof Date );  	// true
	console.log( fn instanceof Function );	  	// true
	注意:instanceof 后面一定要是对象类型,大小写不能写错,该方法试用一些条件选择或分支
	当对象为空时的判断如下:
	let obj1 = {};
	let obj2 = Object.create(null);
	let obj3 = object.create({});
	console.log( obj1 instanceof Object, obj2 instanceof Object, obj3 instanceof Object) // true false true
  1. 使用 object.prototype.toString.call() 方法
    可以精准的返回各种数据类型
 console.log(Object.prototype.toString.call("123"))           -------->[object String]
 console.log(Object.prototype.toString.call(123))             -------->[object Number]
 console.log(Object.prototype.toString.call(true))            -------->[object Boolean]
 console.log(Object.prototype.toString.call([1, 2, 3]))       -------->[object Array]
 console.log(Object.prototype.toString.call(null))            -------->[object Null]
 console.log(Object.prototype.toString.call(undefined))       -------->[object Undefined]
 console.log(Object.prototype.toString.call({name: 'Hello'})) -------->[object Object]
 console.log(Object.prototype.toString.call(function () {}))  -------->[object Function]
 console.log(Object.prototype.toString.call(new Date()))      -------->[object Date]
 console.log(Object.prototype.toString.call(/\d/))            -------->[object RegExp]
 console.log(Object.prototype.toString.call(Symbol()))        -------->[object Symbol]
  1. 根据对象的 constructor 进行判断
    coustructor 判断方法跟 instanceof 相似,但是 constructor 检测 Object 与 instanceof 不一样,constructor 还可以处理基本类型的检测
 console.log([1,2,3].constructor === Array); // true
 console.log(('123').constructor === String); // true

注意:

  • null 和 undefined 没有 constructor
  • 判断数字时使用(),比如 (123).constructor,如果写成 123.constructor 会报错
  • constructor 在 类继承时会报错,因为 Object 被覆盖掉了,检测结果就不对了
  1. 在 JQuery 方法
    jquery.type()
    如果对象是 null 跟 undefined ,直接返回 null 和 undefined。
    注意:在使用时,一定要先引入JQuery 文件,不然会报错 Jquery is not undefined
	console.log(jQuery.type(undefined) === "undefined")           -------->true
    console.log(jQuery.type(window.notDefined) === "undefined")   -------->true
    console.log(jQuery.type(123) === "number")                    -------->true
    console.log(jQuery.type('123') === "string")                  -------->true
    console.log(jQuery.type([]) === "array")                      -------->true
    console.log(jQuery.type(true) === "boolean")                  -------->true
    console.log(jQuery.type(function(){}) === "function")         -------->rue
    console.log(jQuery.type(new Date()) === "date")               -------->true
    console.log(jQuery.type(/\d/) === "regexp")                   -------->true
    console.log(jQuery.type(new Error()) === "error")         -------->true jq版本高于1.9.3
    console.log(jQuery.type({name:'Hello'}) === "object")         ------->true
    console.log(jQuery.type(Symbol()) === "symbol")                ------->true
                                                           ------->其余对象类型一般返回object

其他判断类型的方法

jQuery提供了一系列工具方法,用来判断数据类型,以弥补JavaScript原生的typeof运算符的不足。以下方法对参数进行判断,返回一个布尔值。
jQuery.isArray();是否为数组
jQuery.isEmptyObject();是否为空对象 (不含可枚举属性)。
jQuery.isFunction():是否为函数
jQuery.isNumberic():是否为数字
jQuery.isPlainObject():是否为使用“{}”或“new Object”生成对象,而不是浏览器原生提供的对象。
jQuery.isWindow(): 是否为window对象;
jQuery.isXMLDoc(): 判断一个DOM节点是否处于XML文档中。

原文地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值