js判断数据类型最全的5种方法

一、js数据类型

number  //数字  类型包含整数和浮点数
string  //字符串 字符串有length属性
boolean  //布尔值  该类型只有两个值,true和false
function //函数 通常是使用函数声明语法定义的
object   //对象 一组属性与方法的集合
null    //该类型被看做空对象指针,也是空的对象引用
undefined //只有一个值即undefined值,使用var声明了变量但未给变量初始化值

二、判断数据类型

1.最常见的判断方法:typeof 

typeof 1;//'number'
typeof true;//'boolean'
typeof '';//'string'
typeof undefined;//'undefined'
typeof function (){};'function'

//typeof返回的类型都是字符串形式,需注意,例如:

typeof '' == "string" //true
typeof '' == String  //false

//typeof 可以判断function的类型;在判断除Object类型的对象时比较方便。

//在JS中,能用typeof能检测出来的数据类型,最好使用typeof来检测
//typeof是基本数据类型检测利器(但是不包括null);也可以检测是否为一个函数,但是在检测对象时,
//并不能为每个对象明确指出是属于哪一类,因为返回值都是object。

2.判断已知对象类型的方法: instanceof

//instanceof是一个操作符,返回值是一个布尔值
//instanceof是检测引用数据类型,而不能检测基本数据类型

var arr = [];
var fn = function(){};
arr instanceof Array;//true

//但是只要是在原型链上出现过构造函数都会返回true,所以这个检测结果不很准确
arr instanceof Object;//true

//instanceof 后面一定要是对象类型,并且大小写不能错,适合一些条件选择或分支。
fn instanceof Function // true
fn instanceof function // false

3.根据对象的constructor判断: constructor

//constructor这个属性存在于构造函数的原型上,指向构造函数
//对象可以通过__proto__在其所属类的原型上找到这个属性

var arr = [];
arr.constructor === Array;//true
arr.constructor === Object;//false
//arr通过原型链查找到的constructor指向了Array,所以Object判断是错误的

constructor是比较准确判断对象的所属类型的,但是在类继承时会出错
function Fn() {}
Fn.prototype = new Array();
var fn = new Fn();
fn.constructor === Array; //true
//fn是一个对象,但是它的构造函数指向Array是true

function A(){};
function B(){};
A.prototype = new B(); //A继承自B
var aObj = new A();
aobj.constructor === B //true;
aobj.constructor === A // false;

对象直接继承和间接继承的都会报true
aobj instanceof B //true;
aobj instanceof B //true;

解决construtor的问题通常是让对象的constructor手动指向自己:
aobj.constructor = A; //将自己的类赋值给对象的constructor属性
aobj.constructor === A //true;
aobj.constructor === B //false; //基类不会报true了;

4.繁琐的方法: prototype

//在Object基本类定义的这个toString()方法,是用来检测数据类型的;
//跟字符串、数字、布尔等原型上定义的toString()方法基本用法都是转换字符串的

console.log(Object.prototype.toString.call(1));//[object Number]
console.log(Object.prototype.toString.call(''));//[object String]
console.log(Object.prototype.toString.call(true));//[object Boolean]
console.log(Object.prototype.toString.call(null));// [object Null]
console.log(Object.prototype.toString.call(undefined));//[object Undefined]
console.log(Object.prototype.toString.call([]));// [object Array]
console.log(Object.prototype.toString.call({}));// [object Object]
console.log(Object.prototype.toString.call(/^$/));//[object RegExp]
console.log(Object.prototype.toString.call((function () {})));//[object Function]

//大小写不能写错,比较麻烦,数据检测应该是最准确的。

5.jquery的$.type()

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

//如果对象有一个内部属性[[Class]]和一个浏览器的内置对象的 [[Class]] 相同
//则返回相应的 [[Class]] 名字。
$.type( true ) === "boolean"
$.type( 3 ) === "number"
$.type( "test" ) === "string"
$.type( function(){} ) === "function"
$.type( [] ) === "array"
$.type( new Date() ) === "date"
$.type( new Error() ) === "error" // jQuery 1.9 新增支持
$.type( /test/ ) === "regexp"

//其他一切都将返回它的类型“object”。

//这是个很强的的方法 但是你需要引入jquery 
//或者自己尝试解析jquery的$.type()源码 自己去封装一个类似的方法

//比如说这样:
var class2type={},          
    toString =object.prototype.toString();

function testType(obj){
      return obj ==null ? String(obj) :class2type[toString.call(obj)] || "object";
}
//这个我还没有测试 不知道可不可行 不行的可以告诉我 我再去测试

一般情况用typeof 判断就可以,遇到预知Object类型可以用instanceof或constructor方法,前面都不行的话就用$.type()方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

招来红月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值