一共有4中数据类型判断的方法
1.typeof()他可以检测一些普通数据类型,比较直观的数据,比如number,string这些能够具体的检测出来他们的类型,判断引用数据类型无论引用的对象是什么类型都返回object;
console.log(typeof a); //'undefined'
console.log(typeof(true)); //'boolean'
console.log(typeof '123'); //'string'
console.log(typeof 123); //'number'
console.log(typeof NaN); //'number'
console.log(typeof null); //'object'
var obj = new String();
console.log(typeof(obj)); //'object'
var fn = function(){};
console.log(typeof(fn)); //'function'
console.log(typeof(class c{})); //'function'
2.instanceof 他可以检测引用数据类型,可以判断一个变量是否是某个类型和是否是某个实例属性是否出现在某个实例对象的原型链上,由构造类型判断数据类型,返回的是true或者false,不可以判断null和undefined fn instanceof Fn
let Car = function(){}
let benz = new Car()
benz instanceof Car //true
let car = new String('la')
car instanceof Car //false
car instanceof String //true
3.constructor 他是通过对象的construnctor直接来判断他是否和他的原型相同,但是一旦构造函数的原型被更改那么这个方法就不好用了 fn.constructor == Array
// 字符串:String()
var str = "张三";
alert(str.constructor); // function String() { [native code] }
alert(str.constructor === String); // true
// 数组:Array()
var arr = [1, 2, 3];
alert(arr.constructor); // function Array() { [native code] }
alert(arr.constructor === Array); // true
// 数字:Number()
var num = 5;
alert(num.constructor); // function Number() { [native code] }
alert(num.constructor === Number); // true
// 自定义对象:Person()
function Person(){
this.name = "CodePlayer";
}
var p = new Person();
alert(p.constructor); // function Person(){ this.name = "CodePlayer"; }
alert(p.constructor === Person); // true
// JSON对象:Object()
var o = { "name" : "张三"};
alert(o.constructor); // function Object() { [native code] }
alert(o.constructor === Object); // true
// 自定义函数:Function()
function foo(){
alert("CodePlayer");
}
alert(foo.constructor); // function Function() { [native code] }
alert(foo.constructor === Function); // true
// 函数的原型:bar()
function bar(){
alert("CodePlayer");
}
alert(bar.prototype.constructor); // function bar(){ alert("CodePlayer"); }
alert(bar.prototype.constructor === bar); // true
4.Object.prototype.toString.call() 他目前是一个最完美的解决方案,他只需要在括号里边写你想要判断的数据类型就行了,他可以直接从原型里面找要判断的类型
//判断基本类型:
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, age) {
this.name = name;
this.age = age;
}
var person = new Person("Rose", 18);
Object.prototype.toString.call(arr); //”[object Object]”
//判断原生JSON对象:
var isNativeJSON = window.JSON && Object.prototype.toString.call(JSON);
console.log(isNativeJSON);//输出结果为”[object JSON]”说明JSON是原生的,否则不是;