检测数据类型的五种方法

先列举几种数据类型

var str = "iamstring.";

var num = 222;

var arr= [1,2,3];

var d = new Date();

var f = function(){console.log("function")};

var o=new Object();

1.第一种方法 typeof:

typeof num   --->Number
typeof str...   --->String

 

2.第二种方法 instanceof :
(num instanceof Number) --->true
(str instanceof String) --->true
(arr instanceof Array) --->true
(f instanceof Function) --->true

 

3. 第三种方法 constructor

num.constructor === Number

str.constructor === String

4.第四中方法 prototype

Object.prototype.toString.call(arr) ----> "[object Array]"
Object.prototype.toString.call(arr).slice(8,-1) ---> Array

 

5.第五中方法 jQuery.type

 jQuery.type(str) --->array

jQuery.type(undefined)   --->undefined

jQuery.type()  --->undefined

jQuery.type(null)  --->null

jQuery.type(d)  --->date

 

转载于:https://www.cnblogs.com/crystal2018/p/9045919.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值