数据类型的检测方法

一,typeof

 1     typeof 123, //"number"
 2     typeof 'dsfsf', //"string"
 3     typeof false, //"boolean"
 4     typeof function(){console.log('aaa');}, //"function"
 5     typeof undefined, //"undefined"
 6     
 7     typeof [1,2,3], //"object"
 8     typeof {a:1,b:2,c:3}, //"object" 
 9     typeof null, //"object"
10     typeof new Date(), //"object"
11     typeof /^[a-zA-Z]{5,20}$/, //"object"
12     typeof new Error() //"object"

typeof只能判断:Number,String,Boolean,Function,undefined几种类型;

Array,Object,null,Date,RegExp,Error都被typeof判断为Object类型;

二,instanceof

instanceof需要指定构造函数(类型),来判断这个构造函数的原型是否再给定对象的原型链上

 1     123 instanceof Number, //false
 2     'dsfsf' instanceof String, //false
 3     false instanceof Boolean, //false
 4     undefined instanceof Object, //false
 5     null instanceof Object, //false
 6 
 7     [1,2,3] instanceof Array, //true
 8     {a:1,b:2,c:3} instanceof Object, //true
 9     function(){console.log('aaa');} instanceof Function, //true
10     new Date() instanceof Date, //true
11     /^[a-zA-Z]{5,20}$/ instanceof RegExp, //true
12     new Error() instanceof Error //true

以上形式的Number,String,Boolean判断不出类型,但如果使用new新建,则可以检测出

null和undefined返回false,因为它们的类型就是自己本身,不是object创建出来的,所以返回false

1 var num = new Number(123);
2 var str = new String('dsfsf');
3 var boolean = new Boolean(false);
4 
5 num instanceof Number; //true
6 str  instanceof String; //true
7 boolean instanceof Boolean;//true

三,constructor

constructor是prototype对象上的属性,指向构造函数。根据实例对象寻找属性的顺序,若实例对象没有实例属性或方法时,就去原型链上寻找,

因此实例对象也是能使用constructor属性的。

例:输出Number类型的实例的constructor:

1 console.log(new Number(123).constructor)
2 //f Number(){[native code]}

上例可看出constructor指向了Number的构造函数,因此可以使用num。constructor==Number来判断一个变量是不是Number类型的。

123.constructor == Number;
//Uncaught SyntaxError: Invalid or unexpected token

 必须以变量的形式进行判断

var num  = 123; num.constructor==Number;
//true
var str  = 'abcdef';str.constructor==String;
//true
var bool = true; bool.constructor==Boolean;
//true
var arr  = [1, 2, 3, 4];arr.constructor==Array;
//true
var json = {name:'wenzi', age:25}; json.constructor==Object;
//true
var func = function(){ console.log('this is function'); }; func.constructor==Function;
//true
var date = new Date(); date.constructor==Date;
//true
var reg  = /^[a-zA-Z]{5,20}$/; reg.constructor==RegExp;
//true
var error= new Error();error.constructor==Error;
//true
function Person(){}
var tom = new Person();
tom.constructor==Person;
//true

var und  = undefined;
var nul  = null;
// undefined和null没有constructor属性

 除了undefined和null之外,其他类型都可以通过constructor属性来判断类型

四,toString()

 Object.prototype.toString()可以获取每个对象的类型。但要以Function.prototype.call()或Function.prototype.apply()的形式调用,

传递要检查的对象作为第一个参数thisArg。

 1 var toString = Object.prototype.toString;
 2 
 3 toString.call(123); //"[object Number]"
 4 toString.call('abcdef'); //"[object String]"
 5 toString.call(true); //"[object Boolean]"
 6 toString.call([1, 2, 3, 4]); //"[object Array]"
 7 toString.call({name:'wenzi', age:25}); //"[object Object]"
 8 toString.call(function(){ console.log('this is function'); }); //"[object Function]"
 9 toString.call(undefined); //"[object Undefined]"
10 toString.call(null); //"[object Null]"
11 toString.call(new Date()); //"[object Date]"
12 toString.call(/^[a-zA-Z]{5,20}$/); //"[object RegExp]"
13 toString.call(new Error()); //"[object Error]"

以上,可以看出Object.prototype.toString.call()可以准确的判断变量的类型。

获取变量的准确类型:

Object.prototype.toString.call(obj).replace(/^[object (S+)]$/, '$1')
原文地址:https://www.cnblogs.com/Janejxt/p/14701811.html