JS 类型判断
typeof
操作符,后接一个操作数,用于判断对象或原始值的表达式的数据类型,返回结果是全小写
返回的类型有:number,string,boolean,undefined,object,function,symbol
console.log(typeof 123); //number
console.log(typeof "zs"); //string
console.log(typeof true); //boolean
console.log(typeof undefined); //undefined
console.log(typeof new Object()); //object
console.log(typeof function () { }); //function
console.log(typeof null); //object
console.log(typeof []); //object
console.log(typeof new Date()); //object
console.log(typeof new RegExp());//object
console.log(typeof Symbol());//symbol
instanceof
instanceof运算符用来检测构造函数的prototype属性是否出现在某个实例对象的原型链上
即用来判断per是否Person的实例,若是返回true,不是返回false
function Person(name, age) {
this.name = name;
this.age = age;
}
// 调用自定义构造函数
var per = new Person('zs', 23);
console.log(per); //Person { name: 'zs', age: 23 }
console.log(per instanceof Person); //true
console.log(per instanceof Object); //true
var per1 = new Object()
console.log(per1 instanceof Person); //false
console.log(per1 instanceof Object); //true
constructor
当定义一个函数时,函数会拥有prototype原型,原型中拥有一个constructor属性,指向prototype原型,用constructor判断是否属于某个数据类型,是返回true,不是返回false
console.log("".constructor == String); //true
console.log(new Number(1).constructor == Number); //true
console.log(true.constructor == Boolean); //true
console.log(new Function().constructor == Function); //true
console.log(new Date().constructor == Date); //true
console.log([].constructor == Array); //true
console.log(new RegExp().constructor == RegExp); //true
console.log(new Error().constructor == Error); //true
Object.prototype.toString()
toString()是Object的原型方法,通过调用该方法会返回**[[Class]]**,格式为[object xxx],其中xxx就是该对象的类型
判断Object对象类型可直接调用toString()方法
其他对象需要通过call、apply进行调用
//[object Object]
console.log(Object.prototype.toString(new Object()));
//[object Object]
console.log(Object.prototype.toString.call({ name: 'zs' }));
//[object String]
console.log(Object.prototype.toString.call(''));
//[object Number]
console.log(Object.prototype.toString.call(123));
//[object Array]
console.log(Object.prototype.toString.call([]));
//[object Boolean]
console.log(Object.prototype.toString.call(true));
//[object Undefined]
console.log(Object.prototype.toString.call(undefined));
//[object Null]
console.log(Object.prototype.toString.call(null));
//[object Number]
console.log(Object.prototype.toString.call(NaN));
//[object Function]
console.log(Object.prototype.toString.call(new Function()));
//[object RegExp]
console.log(Object.prototype.toString.call(new RegExp()));
//[object Date]
console.log(Object.prototype.toString.call(new Date()));
//[object Error]
console.log(Object.prototype.toString.call(new Error()));
//[object global]
console.log(Object.prototype.toString.call(global));
jQuery.type()
jQuery的用法有两种:jQuery、$
$.type()/jQuery.type()函数用于确定JS内置对象的数据类型,返回小写形式的数据类型名称
$.type(null) //null
$.type(undefined) //undefined
$.type(123) //number
$.type(new Array()) //array
$.type(new Object()) //object
$.type(new Date()) // date
$.type(new Error()) //error
$.type(new RegExp()) //regexp
$.type(NaN) //number
$.type(['123','kkk']) //array
$.type(true) //boolean
$.type({name:'zs'}) //object
$.type(window) //object
封装函数:用于准确判断数据类型
function getType(obj) {
let type = typeof obj;
if (type != "object") {
return type;
}
return Object.prototype.toString.call(obj);
}