欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

判斷js數(shù)據(jù)類(lèi)型的函數(shù)實(shí)例詳解

 更新時(shí)間:2019年05月23日 14:09:50   作者:史萊姆爆炸了  
這篇文章主要介紹了一個(gè)判斷js數(shù)據(jù)類(lèi)型的函數(shù),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
function judgeType(change) {
    if (arguments.length == 0) {
      return '0';//無(wú)參數(shù)傳入
    }
    if (change === null) {
      return 'null'
    }
    if (change === undefined && arguments.length > 0) {
      return 'undefined'
    }
    if (change instanceof Function) {
      return 'function'
    }
    if (change instanceof Array) {
      return 'arry'
    }
    if (change instanceof Number || typeof change == 'number') {
      return 'number'
    }
    if (change instanceof String || typeof change == 'string') {
      return 'string'
    }
    if (change instanceof Boolean || typeof change == 'boolean') {
      return 'boolean'
    }
  }

ps:下面看下js 判斷各種數(shù)據(jù)類(lèi)型

了解js的都知道, 有個(gè)typeof  用來(lái)判斷各種數(shù)據(jù)類(lèi)型,有兩種寫(xiě)法:typeof   xxx   ,typeof(xxx)

       如下實(shí)例:

 typeof  2   輸出  number
    typeof  null  輸出  object
    typeof  {}  輸出  object
    typeof  []  輸出  object
    typeof  (function(){})  輸出 function 
    typeof  undefined     輸出 undefined
    typeof  '222'         輸出  string 
   typeof true          輸出   boolean 

    這里面包含了js里面的五種數(shù)據(jù)類(lèi)型  number   string    boolean   undefined     object和函數(shù)類(lèi)型 function

     看到這里你肯定會(huì)問(wèn)了:我怎么去區(qū)分對(duì)象,數(shù)組和null呢?

     接下來(lái)我們就用到另外一個(gè)利器:Object.prototype.toString.call

     這是對(duì)象的一個(gè)原生原型擴(kuò)展函數(shù),用來(lái)更精確的區(qū)分?jǐn)?shù)據(jù)類(lèi)型。

     我們來(lái)試試這個(gè)玩兒意兒:

  var  gettype=Object.prototype.toString
    gettype.call('aaaa')    輸出   [object String]
    gettype.call(2222)     輸出   [object Number]
    gettype.call(true)     輸出   [object Boolean]
    gettype.call(undefined) 輸出   [object Undefined]
    gettype.call(null)         輸出  [object Null]
     gettype.call({})          輸出  [object Object]
     gettype.call([])          輸出  [object Array]
     gettype.call(function(){})   輸出  [object Function]

      看到這里,剛才的問(wèn)題我們解決了。

     constructor也能判斷數(shù)據(jù)類(lèi)型:

     如:

''.constructor==String   
      [].constructor==Array
      var obj= new Object()  
  obj.constructor==Object

      其實(shí)js 里面還有好多類(lèi)型判斷      [object HTMLDivElement]     div 對(duì)象  ,    [object HTMLBodyElement]  body 對(duì)象    ,[object Document](IE)或者  [object HTMLDocument](firefox,google) ......各種dom節(jié)點(diǎn)的判斷,這些東西在我們寫(xiě)插件的時(shí)候都會(huì)用到。

     可以封裝的方法如下  :

 var  gettype=Object.prototype.toString
   var  utility={
     isObj:function(o){
        return  gettype.call(o)=="[object Object]";
     },
     isArray:function(o){
        return  gettype.call(o)=="[object Array]";
     },
     isNULL:function(o){
        return  gettype.call(o)=="[object Null]";
     },
     isDocument:function(){
         return  gettype.call(o)=="[object Document]"|| [object HTMLDocument];
     }
     ........
  }

    這個(gè)獲取類(lèi)型的方法有個(gè)簡(jiǎn)單的寫(xiě)法:

var Type = (function() {
        var type = {};
        var typeArr = ['String', 'Object', 'Number', 'Array','Undefined', 'Function', 'Null', 'Symbol'];
        for (var i = 0; i < typeArr.length; i++) {
          (function(name) {
            type['Is' + name] = function(obj) {
              return Object.prototype.toString.call(obj) == '[object ' + name + ']';
            }
          })(typeArr[i]);
        }
        return type;
})(); 

調(diào)用方法:Type.IsFunction(function() {})      Type.IsObject({})。。。。。

Type.Is.....

總結(jié)

以上所述是小編給大家介紹的判斷js數(shù)據(jù)類(lèi)型的函數(shù)實(shí)例詳解,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的!

相關(guān)文章

最新評(píng)論