用来检测数据类型的运算符
语法:typeof[value]
typeof 12 //=>'number' typeof NaN //=>'number' typeof ''=>'string' var flag=true; typeof flag //'boolen' tpeof undefined //=>'undefined' function fn(n,m){ if(typeof n==='undefined'){ } } typeof null //=>'object' //虽然是基本数据类型值,可是它属于空对象指针,检测的结果是对象 typeof {} //=>'object' typeof function(){} //=>'function' typeof [] //=>'object' typeof /^$/ //=>'object' //使用typeof有本身的局限性,不能具体细分当前的值是数组仍是正则(也就是不能细分对象类型的值) typeof (1>1?0:2) //=>'number' typeof 1>1?0:2 //=>先算typeof 1-> 'number'=> 'number'>1?0:2 typeof typeof [] //=>'string' //=>typeof [] 'object' //type of 'object' =>'string'
instanceof : 检测某一个实例是否属于某各种的实例
constructor : 构造函数使用instanceof 检测某个值是否属于某一个数据类型的内置类,从而检测出它是不是这个类型的值;使用instanceof能够实现typeof实现不了的,对对象类型值详细的区分检测;javascript
[] instanceof Array //=>true [] instanceof RegExp //=>false
使用instanceof检测也有本身的弊端:
1.基本类型值没法基于它的检测
var num =12; num.toFixed(2) =>'12.00' //12是Number类的一个实例,能够调取Number.prototype上的方法,可是它是基本类型值 var num2=new Number(12); num2.toFixed(2) =>'12.00' typeof num //=>'number' typeof num2//=>'object' //无论是哪种方式建立基本类型值,都是本身所属类的实例(只不过类型不同而已) num instanceof Number //=>false num2 instanceof Number //=>true
2.instanceof 检测的原理是基于原型链检测的:只要当前类在实例的原型链上,最后返回的结果都是true.
var ary=[]; ary instanceof Array //=>true ary instanceof Object //=>true function Fn(){} Fn.prototype=new Array();//=>原型继承(Fn 是Array的子类) var f=new Fn(); f instanceof Array //=>true 可是f其实不是数组,虽然在它的原型上能够找到数组,可是它不具有数组的基础结构,这也是instanceof的弊端
获取当前检测数据值的constructor,判断它是不是某一个数据类型内置类来检测
var ary=[]; ary.constructor===Array //=>true ary.constructor===RegExp //=>false ary.constructor===Object //=>false ary.constructor='AA' ary.constructor===Array; //false //=>constructor检测数据类型很是不可靠,由于这个属性是常常容易被修改的。
获取Object.prototype上的toString方法,让方法中的this变为须要检测的数据类型值,而且让方法执行在Number、String、Boolean、Array、Function、RexExp...这些类的原型上都有一个toString方法:这个方法就是将自身的值转换为字符串的java
(12).toString() //=>'12' (true).toString() //=>'true' [12,23].toString() //=>'12,23' ...
在Object这个类的原型上也有一个方法toString,可是这个方法并非把值转换为字符串,而是
返回当前值的所属类详细信息[object 所属的类]
var obj={name:'tom'} obj.toString() //=>"[object Object]" 调取的是Object.prototype.toString /* *obj.toString() * 首先执行Object.prototype.toString 方法 * 这个方法的this就是咱们操做的数据值obj * =>总结:Object.prototype.toString执行的时候会返回当前方法中的this的所属类信息 * * 也就是,咱们想知道谁是所属类信息,咱们就把这个toString方法执行,而且让this变为咱们检测的这个数据值,那么方法返回的结果就是当前检测这个值的所属类信息 * Object.prototype.toString.call([value]) * ({}).toString.call([value]) * */
Object.prototype.toString.call(12) //=>'[object Number]' Object.prototype.toString.call(true) //=>'[object Boolean]' Object.prototype.toString.call('') //=>'[object String]' Object.prototype.toString.call({}) //=>'[object Object]' Object.prototype.toString.call(null) //=>'[object Null]' Object.prototype.toString.call([]) //=>'[object Array]' Object.prototype.toString.call(/^$/) //=>'[object RegExp]' Object.prototype.toString.call(function(){}) //=>'[object Function]'
使用toString检测数据类型,无论你是什么类型值,均可以正常检测出须要的结果(这个方法检测是万能的)
alert({name:'tom'}) //[object Object] alert()=>转换为字符串弹出 //若是弹对象字符串 alert(JSON.stringify({name:'tom'}))
~function(){ let obj={ isNumber:'Number', isString:'String', isBoolean:'Boolean', isNull:'Null', isUndefined:'Undefined', isPlanObject:'Object', isArray:'Array', isRegExp:'RegExp', isFunction:'Function' } let check={}; for (let key in obj) { if (obj.hasOwnProperty(key)) { check[key]=(function(classValue){ return function(val){ return new RegExp('\\[object '+classValue+'\\]').test(Object.prototype.toString.call(val)) } })(obj[key]) } } window.check=check; }()