JavaScript類型檢測之typeof 和 instanceof 的缺陷與優(yōu)化
在javascript中,typeof 和 instanceof 是用來判斷數(shù)據(jù)類型比較通用的兩個方法,這篇文章的目的是通過對這兩個方法介紹來分析其存在的不足并提出優(yōu)化方案。
typeof
--------------------------------------------------------------------------------
typeof 返回一個表達式的數(shù)據(jù)類型的字符串,返回結(jié)果為javascript中的基本數(shù)據(jù)類型,包括:number、boolean、string、object、undefined、function等6種數(shù)據(jù)類型。
typeof 100; //number typeof (1==1); //boolean typeof 'onepixel'; //string typeof {} ; //object typeof onepixel; // undefined typeof parseInt; // function typeof [];//object typeof new Date(); //object
可以看出,typeof 可以準確的判斷除object以外的基礎(chǔ)數(shù)據(jù)類型,但不能區(qū)分object類型的具體類型,比如 Array 、Date 以及自定義類。
instanceof
--------------------------------------------------------------------------------
instanceof 本意是用來判斷 A 是否為 B 的實例對象,表達式為:A instanceof B,如果A是B的實例,則返回true,否則返回false。 在這里需要特別注意的是:instanceof檢測的是原型,那它是怎么檢測的呢,我們用一段偽代碼來模擬其內(nèi)部執(zhí)行過程:
instanceof (A,B) = { var L = A.__proto__; var R = B.prototype; if(L === R) { //A的內(nèi)部屬性__proto__指向B的原型對象 return true; } return false; }
從上述過程可以看出,當A的__proto__ 指向B的prototype時,就認為A就是B的實例對象,我們再來看幾個例子:
[] instanceof Array; //true {} instanceof Object;//true new Date() instanceof Date;//true function Person(){}; new Person() instanceof Person; [] instanceof Object; //true new Date() instanceof Object;//tru new Person instanceof Object;//true
從上面的例子中,我們發(fā)現(xiàn)雖然instanceof能夠正確判斷[] 是Array的實例對象,但不能辨別 [] 不是Object的實例對象,為什么呢,這還需要從javascript的原型鏈說起,我們首先來分析一下[]、Array、Object 三者之間的關(guān)系,從instanceof判斷能夠得出:[].__proto__ ->Array.prototype, 而Array.prototype.__proto__指向了Object.prototype,Object.prototype.__proto__ 指向了null,標志著原型鏈的結(jié)束。(ps:關(guān)于JS原型鏈請閱讀:淺談javascript原型和原型鏈) 因此,[]、Array、Object就形成了一條原型鏈:
從原型鏈可以看出,[]的__proto__最終指向了Object.prototype,類似的new Date()、new Person() 也會形成這樣一條原型鏈,因此,我們用 instanceof 也不能完全精確的判斷object類的具體數(shù)據(jù)類型。
優(yōu)化方案
--------------------------------------------------------------------------------
對于這個問題,在閱讀jQuery源碼時,發(fā)現(xiàn)了一個比較好的解決方案,由于源碼之間存在相互調(diào)用不便于閱讀和理解,因此,按照其思路進行了整理和封裝,代碼如下:
(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 常用來判斷一個變量是否為空,或者是什么類型的。但它們之間還是有區(qū)別的:
typeof
typeof 是一個一元運算,放在一個運算數(shù)之前,運算數(shù)可以是任意類型。
它返回值是一個字符串,該字符串說明運算數(shù)的類型。typeof 一般只能返回如下幾個結(jié)果:
number,boolean,string,function,object,undefined。我們可以使用 typeof 來獲取一個變量是否存在,如 if(typeof a!="undefined"){alert("ok")},而不要去使用 if(a) 因為如果 a 不存在(未聲明)則會出錯,對于 Array,Null 等特殊對象使用 typeof 一律返回 object,這正是 typeof 的局限性。
網(wǎng)上的一個小例子:
<!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>
instanceof
instance:實例,例子
a instanceof b?alert("true"):alert("false"); //a是b的實例?真:假
instanceof 用于判斷一個變量是否某個對象的實例,如 var a=new Array();alert(a instanceof Array); 會返回 true,同時 alert(a instanceof Object) 也會返回 true;這是因為 Array 是 object 的子類。再如:function test(){};var a=new test();alert(a instanceof test) 會返回
談到 instanceof 我們要多插入一個問題,就是 function 的 arguments,我們大家也許都認為 arguments 是一個 Array,但如果使用 instaceof 去測試會發(fā)現(xiàn) arguments 不是一個 Array 對象,盡管看起來很像。
另外:
測試 var a=new Array();if (a instanceof Object) alert('Y');else alert('N');
得'Y'
但 if (window instanceof Object) alert('Y');else alert('N');
得'N'
所以,這里的 instanceof 測試的 object 是指 js 語法中的 object,不是指 dom 模型對象。
使用 typeof 會有些區(qū)別
alert(typeof(window)) 會得 object
大家知道JavaScript中判斷函數(shù)參數(shù)類型是用typeof還是instanceof嗎?
typeof只能判斷js已有的幾個類型,如function,object,number。
而instanceof可以判斷對象是由哪個函數(shù)實例化出來的,如:
var 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。
而用typeof c和typeof d的結(jié)果都是object
“判斷函數(shù)參數(shù)類型”需要根據(jù)你的需求來選擇用哪個。
相關(guān)文章
javascript 身份證號碼驗證函數(shù)(可辨真?zhèn)危С?5或18位身份證號)測試誤差為0
javascript 身份證號碼驗證函數(shù)(可辨真?zhèn)?,支?5或18位身份證號)測試誤差為0 ,非常不錯,其實主要是特別熟悉身份證的算法。2010-05-05iframe中子父類窗口調(diào)用JS的方法及注意事項
本文給大家介紹iframe中子父類窗口調(diào)用JS的方法及注意事項,介紹的超詳細,需要的朋友快來學習下2015-08-08Websocket通信協(xié)議在數(shù)字孿生中的應(yīng)用
這篇文章主要為大家介紹了Websocket通信協(xié)議在數(shù)字孿生中的應(yīng)用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07javascript中的nextSibling使用陷(da)阱(keng)
關(guān)于HTML/XML節(jié)點的問題,在IE中nextSibling不會返回文本節(jié)點,而chrome或者firefox等會返回文本節(jié)點2014-05-05JavaScript實現(xiàn)復(fù)制功能各瀏覽器支持情況實測
這兩天在做Web前端時,遇到需求通過js實現(xiàn)文本復(fù)制的功能,下面與大家分享下各瀏覽器對復(fù)制功能的支持情況,感興趣的朋友可以參考下哈2013-07-07