数据类型判断

12 篇文章 0 订阅

数据类型判断

几种判断数据类型的方法

typeoftypeof 对于基本数据类型的判断是没有问题的,但在判断引用数据类型(如:Array,Object,null)是没有用的。
Instanceofinstanceof 判断new关键字创建的引用数据类型但它在做判断时不考虑null和undefined以及以对象字面量创建的基本数据类型
constructor它既可以对进本数据类型进行判断也可以对引用数据类型进行判断,但它也是有缺点的,constructor不能用于undefined和null因为它们没有构造函数
Object.prototype.toString.call()它既可以用来判断基本数据类型也可以用来判断原生引用类型

 

 

 

 

 

 

 

下面是它们的用法

1. typeof:

typeof value 返回值是value的数据类型 ,比如:“number”、“string”、“boolean”、“undefined”、“object”、“function”

缺点: 在检测null时 typeof null 的返回值为object;在检测数组和正则表达式时返回值都是 “object” ,所以typeof不能检测一个值是否为数组。

2. instanceof/constructor

检测某个实例是否属于某个类,所以在检测数组和正则表达式时就可以用到instanceof/constructor了,比如:

console.log([] instanceof Array) // true 
console.log({} instanceof Object) //true
//constructor 可以避免instanceof在检测时,用Obect也是true的问题 
console.log([].constructor === Object) //true

在使用constructor时:

var arr = [1,2,3,4];
console.log(arr.constructor.name) //Array
//可以使用value.constuructor.name直接返回value的数据类型

缺点:

1,用instanceof检测的时候,只要当前的这个类在实例的原型链上(可以通过原型链__proto__找到它),检测出来的结果都是true

	HTMLDivElement->HTMLElement->Element->Node->EventTarget->Object
		var El = document.getElementById("div");
		HTMLDivElement->HTMLElement->Element->Node->EventTarget->Object
		console.log(El instanceof HTMLDivElement); // true
		console.log(El instanceof Node); // true
		console.log(El instanceof Object); // true

2,基本数据类型的值是不能用instanceof来检测的

	console.log(123 instanceof Number); // false
	console.log('123' instanceof string); // false

3 . Object.prototype.toString.call()

使用Object.prototype上的原生toString()方法判断数据类型

使用方法:Object.prototype.toString.call(value)

判断基本类型时

Object.prototype.toString.call(null); //[object Null]
Object.prototype.toString.call(undefined); //[object Undefined]
Object.prototype.toString.call(“abc”); //[object String]
Object.prototype.toString.call(123); //[object Number]
Object.prototype.toString.call(true); //[object Boolean]

判断原生引用类型

函数类型:

Function fn(){console.log(“test”);}
Object.prototype.toString.call(fn); //[object Function]
日期类型:
var date = new Date();
Object.prototype.toString.call(date); //[object Date]

数组类型:

var arr = [1,2,3];
Object.prototype.toString.call(arr); //[object Array]

正则表达式:

var reg = /[hbc]at/gi;
Object.prototype.toString.call(arr); //[object Array]

自定义:

function Person(name, sex) {
    this.name = name;
    this.sex= sex;
}
var person = new Person("zhangsan", man);
Object.prototype.toString.call(person ); //[object Object]

判断原生JSON对象:

var newJSON =  window.JSON && Object.prototype.toString.call(JSON);
console.log(newJSON) //[object JSON]
// 输出结果为[object JSON]说明JSON是原生的,否则说明不是原生。

4,$.type()

在jq中的判断数据类型的方法 它可以判断所有的数据类型

//$.type() 函数用于确定JavaScript内置对象的类型,并返回小写形式的类型名称。
//如果对象是undefined或null,则返回相应的"undefined"或"null"
$.type(undefined) === "undefined"

$.type() === "undefined"

$.type(window.defined) === "undefined"

$.type(null) === "null"
//如果对象有一个内部属性[[Class]]和一个浏览器的内置对象的 [[Class]] 相同,
//我们返回相应的[[Class]] 名
$.type(true) === "boolean"

$.type(1) === "number"

$.type("test") === "string"

$.type(function(){}) === "function"

$.type([]) === "array"

$.type(new Date()) === "date"

$.type(/test/) === "regexp"

$.type(new Error()) === "error" // jQuery 1.9 新增支持

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值