检测数据类型的五种方法

先列举几种数据类型

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

 

posted @ 2018-05-16 14:38  crystal2018  阅读(196)  评论(0编辑  收藏  举报