使用JS如何判斷是否為數(shù)組-創(chuàng)新互聯(lián)

今天小編給大家分享的是使用JS如何判斷是否為數(shù)組,相信很多人都不太了解,為了讓大家更加了解,所以給大家總結了以下內(nèi)容,一起往下看吧。一定會有所收獲的哦。

創(chuàng)新互聯(lián)建站堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都做網(wǎng)站、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務,滿足客戶于互聯(lián)網(wǎng)時代的楚雄州網(wǎng)站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡建設合作伙伴!

一、Array.isArray判斷

用法:Array.isArray(arr)

ES5中新增了Array.isArray方法,IE8及以下不支持

Array.isArray() 用于確定傳遞的值是否是一個[Array], 返回布爾值 true;否則它返回 false。

let arr = [];
console.log(Array.isArray(arr)); // true
// 下面的函數(shù)調(diào)用都返回 true
Array.isArray([]);
Array.isArray([1]);
Array.isArray(new Array());
Array.isArray(new Array('a', 'b', 'c', 'd'))
// 鮮為人知的事實:其實 Array.prototype 也是一個數(shù)組。
Array.isArray(Array.prototype);

二、constructor判斷

用法:arr.constructor === Array

Object的每個實例都有構造函數(shù)constructor,用于保存著用于創(chuàng)建當前對象的函數(shù)

let arr = [];
console.log(arr.constructor === Array); // true

三、instanceof 判斷

用法:arr instanceof Array

instanceof 主要是用來判斷某個實例是否屬于某個對象

let arr = [];
console.log(arr instanceof Array); // true

注:instanceof操作符的問題在于,它假定只有一個全局環(huán)境。如果網(wǎng)頁中包含多個框架,那實際上就存在兩個以上不同的全局執(zhí)行環(huán)境,從而存在兩個以上不同版本的Array構造函數(shù)。如果你從一個框架向另一個框架傳入一個數(shù)組,那么傳入的數(shù)組與在第二個框架中原生創(chuàng)建的數(shù)組分別具有各自不同的構造函數(shù)。(紅寶書88頁上的原話)

四、原型鏈上的isPrototypeOf判斷

用法:Array.prototype.isPrototypeOf(arr)

Array.prototype  屬性表示 Array 構造函數(shù)的原型

isPrototypeOf()可以用于測試一個對象是否存在于另一個對象的原型鏈上。

let arr = [];
console.log(Array.prototype.isPrototypeOf(arr)); // true

五、Object.prototype.toString

用法:Object.prototype.toString.call(arr) === '[object Array]'

Array繼承自Object,JavaScript在Array.prototype上重寫了toString,toString.call(arr)實際上是通過原型鏈調(diào)用了。

let arr = [];
console.log(Object.prototype.toString.call(arr) === '[object Array]'); // true

六、Array 原型鏈上的 isPrototypeOf

用法:Array.prototype.isPrototypeOf(arr)

Array.prototype  屬性表示 Array 構造函數(shù)的原型

let arr = [];
console.log(Array.prototype.isPrototypeOf(arr)); // true

順便復習一下typeof的用法:

對于引用類型,不能用typeof來判斷,因為返回的都是object

// 基本類型
typeof 123;  //number
typeof "abc"; //string
typeof true; //boolean
typeof undefined; //undefined
typeof null; //object
var s = Symbol;
typeof s; //symbol

// 引用類型
typeof [1,2,3]; //object
typeof {}; //object
typeof function(){}; //function
typeof  Array; //function  Array類型的構造函數(shù)
typeof Object; //function  Object類型的構造函數(shù)
typeof Symbol; //function  Symbol類型的構造函數(shù)
typeof Number; //function  Number類型的構造函數(shù)
typeof String; //function  String類型的構造函數(shù)
typeof Boolean; //function  Boolean類型的構造函數(shù)

關于使用JS如何判斷是否為數(shù)組就分享到這里了,希望以上內(nèi)容可以對大家有一定的參考價值,可以學以致用。如果喜歡本篇文章,不妨把它分享出去讓更多的人看到。

當前名稱:使用JS如何判斷是否為數(shù)組-創(chuàng)新互聯(lián)
URL網(wǎng)址:http://bm7419.com/article22/ddhicc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供ChatGPT營銷型網(wǎng)站建設、網(wǎng)站排名、外貿(mào)網(wǎng)站建設、Google、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司