亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

JavaScript基于自定義函數(shù)判斷變量類型的實(shí)現(xiàn)方法

 更新時(shí)間:2016年11月23日 10:47:29   作者:cuixiping  
這篇文章主要介紹了JavaScript基于自定義函數(shù)判斷變量類型的實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了javascript判斷變量類型的自定義函數(shù)定義與使用方法,并針對(duì)不同瀏覽器給出了相關(guān)的分析與說(shuō)明,需要的朋友可以參考下

本文實(shí)例講述了JavaScript基于自定義函數(shù)判斷變量類型的實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:

通常用typeof來(lái)判斷js變量的類型,但很多時(shí)候僅僅typeof滿足不了要求的。

我寫了一個(gè)自定義函數(shù)來(lái)做這個(gè)事,判斷的比較全面了。

function varType(v){
 if ( typeof v=== "object" ){
  if (v=== null ) return 'null' ;
  if (v. constructor )
   return (v. constructor .toString()).match(/(?: )[/w/$]+/)[ 0 ];
  if ( typeof typeof2=== 'undefined' && window .execScript){
   window .execScript( 'Function vbsTypeName(o):vbsTypeName=TypeName(o):End Function' , 'vbscript' );
   window .execScript( 'function typeof2(o){return vbsTypeName(o)}' , 'jscript' );
  }
  if ( typeof typeof2!== 'undefined' ){
   return typeof2(v);
  }
  return "object" ;
 }
 return typeof v;
}
//對(duì)于普通js常量和js對(duì)象,各瀏覽器是基本一致的
alert (varType()); //undefined
alert (varType( 100 )); //number
alert (varType({})); //Object
alert (varType([])); //Array
alert (varType(/ /)); //RegExp
alert (varType( new Date ())); //Date
alert (varType( Date )); //function
alert (varType( Object )); //function
alert (varType( RegExp )); //function
//對(duì)于DOM對(duì)象,各瀏覽器可能會(huì)有不同值
alert (varType( window )); //IE:HTMLWindow2 FF:Window
alert (varType( document )); //IE:HTMLDocument FF:HTMLDocument
alert (varType( document .body)); //IE:HTMLBody FF:HTMLBodyElement
alert (varType( Option )); //IE:Object FF:function
alert (varType( Image )); //IE:Object FF:function
alert (varType( navigator )); //IE:DispHTMLNavigator FF:Navigator
//以下幾個(gè)只適用于IE,其他內(nèi)核瀏覽器不支持
alert (varType( ActiveXObject )); //IE:function
alert (varType( Enumerator )); //IE:function
alert (varType( new ActiveXObject ( "Scripting.Dictionary" ))); //IE:Dictionary
alert (varType( new Enumerator ())); //IE:Enumerator

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》及《JavaScript遍歷算法與技巧總結(jié)

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論