typeof 100; //numbertypeof (1==1); //booleantypeof "onepixel"; //stringtypeof {} ; //objecttypeof onepixel; // undefinedtypeof parseInt; // functiontypeof [];//objecttypeof new Date(); //object 可以看出,typeof 可以准确的判断除object以外的基础数据类型,但不能区分object类型的具体类型,比如 Array 、Date 以及自定义类。instanceof (A,B) = {var L = A.__proto__;var R = B.prototype;if(L === R) {//A的内部属性__proto__指向B的原型对象return true;}return false;} 从上述过程可以看出,当A的__proto__ 指向B的prototype时,就认为A就是B的实例对象,我们再来看几个例子:[] instanceof Array; //true{} instanceof Object;//truenew Date() instanceof Date;//truefunction Person(){};new Person() instanceof Person;[] instanceof Object; //truenew Date() instanceof Object;//trunew Person instanceof Object;//true 从上面的例子中,我们发现虽然instanceof能够正确判断[] 是Array的实例对象,但不能辨别 [] 不是Object的实例对象,为什么呢,这还需要从javascript的原型链说起,我们首先来分析一下[]、Array、Object 三者之间的关系,从instanceof判断能够得出:[].__proto__ ->Array.prototype, 而Array.prototype.__proto__指向了Object.prototype,Object.prototype.__proto__ 指向了null,标志着原型链的结束。(ps:关于JS原型链请阅读:浅谈javascript原型和原型链) 因此,[]、Array、Object就形成了一条原型链:
从原型链可以看出,[]的__proto__最终指向了Object.prototype,类似的new Date()、new Person() 也会形成这样一条原型链,因此,我们用 instanceof 也不能完全精确的判断object类的具体数据类型。
优化方案
--------------------------------------------------------------------------------
对于这个问题,在阅读jQuery源码时,发现了一个比较好的解决方案,由于源码之间存在相互调用不便于阅读和理解,因此,按照其思路进行了整理和封装,代码如下:
(function(){var class2type = {};var typeList = "Boolean Number String Function Array Date RegExp Object Error Symbol".split( " " );typeList.eachEach(function(item){class2type[ "[object " + item + "]" ] = item.toLowerCase();}return {getObjType:function(obj) {if ( obj == null ) {return obj + "";}if(typeof obj === "object" || typeof obj === "function"){class2type[ toString.call( obj ) ] || "object"}else {return typeof obj;}}}})()JavaScript 中 typeof 和 instanceof 常用来判断一个变量是否为空,或者是什么类型的。但它们之间还是有区别的:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><script language="javascript" type="text/javascript">document.write ("typeof(1): "+typeof(1)+"<br>");document.write ("typeof(NaN): "+typeof(NaN)+"<br>");document.write ("typeof(Number.MIN_VALUE): "+typeof(Number.MIN_VALUE)+"<br>");document.write ("typeof(Infinity): "+typeof(Infinity)+"<br>");document.write ("typeof("123"): "+typeof("123")+"<br>");document.write ("typeof(true): "+typeof(true)+"<br>");document.write ("typeof(window): "+typeof(window)+"<br>");document.write ("typeof(Array()): "+typeof(new Array())+"<br>");document.write ("typeof(function(){}): "+typeof(function(){})+"<br>");document.write ("typeof(document): "+typeof(document)+"<br>");document.write ("typeof(null): "+typeof(null)+"<br>");document.write ("typeof(eval): "+typeof(eval)+"<br>");document.write ("typeof(Date): "+typeof(Date)+"<br>");document.write ("typeof(sss): "+typeof(sss)+"<br>");document.write ("typeof(undefined): "+typeof(undefined)+"<br>")</script><title>javascript类型测试</title></head><body></body></html>instanceofvar a=function(x){};var b=function(x){};var c=new a(1);var d=new a(2);c instanceof a为true而d instanceof b为false。