<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          如何通過(guò)JS判斷一個(gè)數(shù)組

          共 3020字,需瀏覽 7分鐘

           ·

          2023-08-27 07:47

          前言

          如何通過(guò) js 判斷一個(gè)數(shù)組,在面試中我們常常會(huì)遇到這個(gè)問(wèn)題,本章主要基于幾種判斷方式及原理帶大家了解如何通過(guò) js 判斷一個(gè)數(shù)組。

          1. instanceof

          instanceof   用于檢測(cè)構(gòu)造函數(shù)的 prototype 屬性是否在實(shí)例對(duì)象的原型鏈上。如果存在就返回 true否則返回 false

          HTML代碼

                                        
                                          var arr = [];
                                        
                                        
                                          arr instanceof Array; // true
                                        
                                      

          2. constructor

          大家都知道,實(shí)例的構(gòu)造函數(shù)屬性 constructor 指向構(gòu)造函數(shù),那么通過(guò) constructor 屬性也可以判斷是否為一個(gè)數(shù)組。

          HTML代碼

                                        
                                          var arr = [];
                                        
                                        
                                          arr.constructor == Array; //true
                                        
                                      

          3. Array.isArray()

          Array.isArray()   用于確定傳遞的值是否是一個(gè)數(shù)組,返回一個(gè)布爾值。如果 是數(shù)組返回 true;否則返回  false

          HTML代碼

                                        
                                          var a = new Array(123);
                                        
                                        
                                          var b = new Date();
                                        
                                        
                                          console.log(Array.isArray(a)); //true
                                        
                                        
                                          console.log(Array.isArray(b)); //false
                                        
                                      

          4. object.prototype.tostring.call()

          通過(guò) tostring 判斷,每個(gè)對(duì)象都可以通過(guò) object.prototype.tostring.call() 來(lái)檢測(cè)。

          HTML代碼

                                        
                                          let a = [1,2,3]
                                        
                                        
                                          Object.prototype.toString.call(a) === '[object Array]';//true
                                        
                                      
          object.prototype.tostring.call()   強(qiáng)大的地方在于它不僅僅可以檢驗(yàn)是否為數(shù)組,還可以檢測(cè)是否為函數(shù),是否是數(shù)字等等。
                                        
                                          
                                            //檢驗(yàn)是否是函數(shù)
                                          
                                        
                                        
                                          let a = function () {};
                                        
                                        
                                          Object.prototype.toString.call(a) === '[object Function]';//true
                                        
                                        
                                          
                                            //檢驗(yàn)是否是數(shù)字
                                          
                                        
                                        
                                          let b = 1;
                                        
                                        
                                          Object.prototype.toString.call(b) === '[object Number]';//true
                                        
                                      
          e90692b14c7043f1837f21909c6104ef.webp

          以上就是今天的全部?jī)?nèi)容啦,我們下期再見!

          瀏覽 28
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产精品一线 | 免费大香蕉毛片 | 亚洲黄色成人网 | 精品成人无码久久久久 | 日韩xxxxxxxxx |