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

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

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

一、Array.isArray判斷

用法:Array.isArray(arr)

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

Array.isArray() 用于確定傳遞的值是否是一個(gè)[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'))
// 鮮為人知的事實(shí):其實(shí) Array.prototype 也是一個(gè)數(shù)組。
Array.isArray(Array.prototype);

二、constructor判斷

用法:arr.constructor === Array

Object的每個(gè)實(shí)例都有構(gòu)造函數(shù)constructor,用于保存著用于創(chuàng)建當(dāng)前對(duì)象的函數(shù)

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

三、instanceof 判斷

用法:arr instanceof Array

instanceof 主要是用來(lái)判斷某個(gè)實(shí)例是否屬于某個(gè)對(duì)象

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

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

四、原型鏈上的isPrototypeOf判斷

用法:Array.prototype.isPrototypeOf(arr)

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

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

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

五、Object.prototype.toString

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

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

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

六、Array 原型鏈上的 isPrototypeOf

用法:Array.prototype.isPrototypeOf(arr)

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

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

順便復(fù)習(xí)一下typeof的用法:

對(duì)于引用類型,不能用typeof來(lái)判斷,因?yàn)榉祷氐亩际莖bject

// 基本類型
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類型的構(gòu)造函數(shù)
typeof Object; //function  Object類型的構(gòu)造函數(shù)
typeof Symbol; //function  Symbol類型的構(gòu)造函數(shù)
typeof Number; //function  Number類型的構(gòu)造函數(shù)
typeof String; //function  String類型的構(gòu)造函數(shù)
typeof Boolean; //function  Boolean類型的構(gòu)造函數(shù)

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

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

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

廣告

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

網(wǎng)站建設(shè)網(wǎng)站維護(hù)公司