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

JavaScript判斷是否為數(shù)組的各種方法匯總

 更新時(shí)間:2021年08月23日 12:46:31   作者:編程三昧  
如何判斷一個(gè)對(duì)象或一個(gè)值是否是一個(gè)數(shù)組,在面試或工作中我們常常會(huì)遇到這個(gè)問題,既然出現(xiàn)頻率高,想著還是做個(gè)整理,所以這篇文章主要給大家匯總介紹了關(guān)于JavaScript判斷是否為數(shù)組的各種方法,需要的朋友可以參考下

前言

我們?cè)谌粘i_發(fā)中,常常有判斷某值類型的需求,今天我們總結(jié)一下常見的幾種用來判斷是否為數(shù)組的 JavaScript 方法。

Array.isArray

Array.isArray() 是ES5新增的方法,用于確定傳遞的值是否是一個(gè)數(shù)組,如果是數(shù)組,則返回  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"));

需要注意的一點(diǎn)是:其實(shí) Array.prototype 也是一個(gè)數(shù)組。

Array.isArray(Array.prototype); // true

下面的函數(shù)調(diào)用都返回 false:

Array.isArray();
Array.isArray({});
Array.isArray(null);
Array.isArray(undefined);
Array.isArray(17);
Array.isArray('Array');
Array.isArray(true);
Array.isArray(false);
Array.isArray(new Uint8Array(32))
Array.isArray({ __proto__: Array.prototype });

兼容性如下圖:

可以看到,新版的主流瀏覽器都是支持該方法的,可以放心使用。

constructor

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

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

需要注意的是,constructor 有被修改的風(fēng)險(xiǎn),判斷結(jié)果不一定準(zhǔn)確,比如:

let arr = [1, 2, 3];
arr.constructor = function () { }
console.log(arr.constructor === Array); // false

一般不推薦使用 constructor 來判斷是否為數(shù)組,我們只需要知道有這么一個(gè)方法就行。

instanceof

instanceof 運(yùn)算符用于檢測(cè)構(gòu)造函數(shù)的 prototype 屬性是否出現(xiàn)在某個(gè)實(shí)例對(duì)象的原型鏈上。舉個(gè)例子:

// 定義構(gòu)造函數(shù)
function C() {}
function D() {}

var o = new C();

o instanceof C; // true,因?yàn)?Object.getPrototypeOf(o) === C.prototype

o instanceof D; // false,因?yàn)?D.prototype 不在 o 的原型鏈上

o instanceof Object; // true,因?yàn)?Object.prototype.isPrototypeOf(o) 返回 true
C.prototype instanceof Object; // true,同上

用 instanceof 來判斷是否為數(shù)組的用法如下:

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

使用 instanceof 需要注意兩點(diǎn):

  • 構(gòu)造函數(shù)的 prototype 和 實(shí)例的原型鏈都有可能會(huì)改變,所以判斷出的結(jié)果不一定一成不變。
  • 在有 iframe 的頁面腳本中使用 instanceof,可能會(huì)得到錯(cuò)誤的結(jié)果,因?yàn)?iframe 擁有獨(dú)立的全局環(huán)境,不同的全局環(huán)境擁有不同的全局對(duì)象,從而擁有不同的內(nèi)置類型構(gòu)造函數(shù)。

isPrototypeOf

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

function Foo() {}
function Bar() {}
function Baz() {}

Bar.prototype = Object.create(Foo.prototype);
Baz.prototype = Object.create(Bar.prototype);

var baz = new Baz();

console.log(Baz.prototype.isPrototypeOf(baz)); // true
console.log(Bar.prototype.isPrototypeOf(baz)); // true
console.log(Foo.prototype.isPrototypeOf(baz)); // true
console.log(Object.prototype.isPrototypeOf(baz)); // true

如果要用 isPrototypeOf 來判斷傳入?yún)?shù)是否為數(shù)組,可以這樣用:

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

Object.prototype.toString

每個(gè)對(duì)象都有一個(gè) toString() 方法,當(dāng)該對(duì)象被表示為一個(gè)文本值時(shí),或者一個(gè)對(duì)象以預(yù)期的字符串方式引用時(shí)自動(dòng)調(diào)用。

默認(rèn)情況下,toString() 方法被每個(gè) Object 對(duì)象繼承。如果此方法在自定義對(duì)象中未被覆蓋,toString() 返回 "[object type]" 字符串,其中 type 是對(duì)象的類型。

可以通過 toString() 來獲取每個(gè)對(duì)象的類型。為了每個(gè)對(duì)象都能通過 Object.prototype.toString() 來檢測(cè),需要以 Function.prototype.call() 或者 Function.prototype.apply() 的形式來調(diào)用,傳遞要檢查的對(duì)象作為第一個(gè)參數(shù),稱為 thisArg。用法如下:

var toString = Object.prototype.toString;

toString.call(new Date); // [object Date]
toString.call(new String); // [object String]
toString.call(Math); // [object Math]

//Since JavaScript 1.8.5
toString.call(undefined); // [object Undefined]
toString.call(null); // [object Null]

如果要用來判斷一個(gè)對(duì)象是否為數(shù)組,可這樣使用:

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

兼容性如下:

typeof

說到判斷類型,可能很多人都會(huì)想到 typeof 方法,我們這里來復(fù)習(xí)一下 typeof 相關(guān)內(nèi)容。

typeof 操作符返回一個(gè)字符串,表示未經(jīng)計(jì)算的操作數(shù)的類型。

console.log(typeof 42); // "number"
console.log(typeof 'blubber'); // "string"
console.log(typeof true); // "boolean"
console.log(typeof undeclaredVariable); // "undefined"

typeof 可能的返回值如下:

通過上圖可以看到,數(shù)組對(duì)象屬于“其他任何對(duì)象”,所以數(shù)組對(duì)象的 typeof 返回值是 “object”:

let arr = [];
console.log(typeof arr); // "object"

所以,我們要盡量避免使用 typeof。

總結(jié)

以上就是幾種用來判斷一個(gè)值是否為數(shù)組的幾種方法,當(dāng)然有好用的也有不好用的,但是不管怎樣,我們知道有這么回事總歸是好的。總結(jié)一下:

  • 最好用的方法是 Array.isArray,只是不支持 IE8 及以下。
  • 如果要考慮兼容性,則可以使用 Object.prototype.toString。

到此這篇關(guān)于JavaScript判斷是否為數(shù)組的文章就介紹到這了,更多相關(guān)JavaScript判斷是否為數(shù)組內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論