判斷一個變量是對象還是數(shù)組的方法

怎么判斷一個對象是不是數(shù)組類型

alert(typeof 1);? ? ? ? ? ? ? ? // 返回字符串"number"

alert(typeof "1");? ? ? ? ? ? ? // 返回字符串"string"

alert(typeof true);? ? ? ? ? ? // 返回字符串"boolean"

alert(typeof undefined);? ? ? ? // 返回字符串"undefined"

alert(typeof function(){});? ? // 返回字符串"function"

alert(typeof {});? ? ? ? ? ? ? // 返回字符串"object"

alert(typeof []);? ? ? ? ? ? ? // 返回字符串"object "

alert(typeof null);? ? ? ? ? ? // 返回字符串"object"

其中,typeof {}和typeof []的結(jié)果都是object,那么問題來了,我怎么通過typeof去判斷一個對象是不是數(shù)組類型呢?

對象是對象,數(shù)組也是對象,js中萬物皆對象,很顯然,通過簡單的typeof運算符是不能夠達到目的,我們得換個方法。

可以嘗試以下幾種方法:

1、從原型入手,Array.prototype.isPrototypeOf(obj);

利用isPrototypeOf()方法,判定Array是不是在obj的原型鏈中,如果是,則返回true,否則false。

Array.prototype.isPrototypeOf([]);//true

Array.prototype.isPrototypeOf({});//false

2、也可以從構(gòu)造函數(shù)入手,obj instanceof Array

先說說 typeof 和 instanceof 的區(qū)別?

兩者都可以用來判斷變量,typeof會返回基本類型,如文章開頭,我們很簡單可以用

typeof a != 'undefined' 判斷a變量存在。而instanceof只會返回一個布爾值,那么我們試試,結(jié)果如下:

"a"? instanceof? Array;? //false

[] instanceof Array ; //true

上面的方法其實也可以用對象constructor屬性,因為在js中每個對象都有constructor屬性,這種也常用作判斷未知對象的類型。

例如:typeof arr == "object" && arr.constructor == Array; //先判斷是對象再進一步判斷。

那樣是不是這個方法就ok了?別開心那么早,坑總是會有的。

在一些跨框架的頁面中的數(shù)組,使用該方法可能不會那么順利,原因是在不同的框架中創(chuàng)建的數(shù)組不會相互共享其prototype屬性。

3、Array.isArray()方法。

Array.isArray([])//true

Array.isArray("123")//false


-----------------------------------------------------------------------------------------------------------------------------------------------------

// 判斷一個變量是對象 還是數(shù)組

? ? ? //第一種方法 instance of ()

? ? ? var arr1 = [1, 2, 3];

? ? ? var Obj1 = { name: "羊要飛", age: "23" };

? ? ? console.log(arr1 instanceof Array); //true

? ? ? console.log(Obj1 instanceof Object); // false

? ? ? console.log("----------------------")


? ? ? // 第二種方法 通過constructor

? ? ? var arr2 = [2, 3, 4];

? ? ? var obj2 = { name: "楊家將", heros: "楊六郎" };

? ? ? console.log(arr2.constructor === Array); // true

? ? ? console.log(obj2.constructor === Array); // false

? ? ? console.log("----------------------")


? ? ? // 第三種方法

? ? ? var arr3 = [1, 2, 3, 1];

? ? ? var arr4 = [{ abac: 1, abc: 2 }];

? ? ? function isArrayFn(value) {

? ? ? ? if (typeof Array.isArray === "function") {

? ? ? ? ? return Array.isArray(value);

? ? ? ? } else {

? ? ? ? ? return Object.prototype.toString.call(value) === "[object Array]";

? ? ? ? }

? ? ? }

? ? ? console.log(isArrayFn(arr3)); // true

? ? ? console.log(isArrayFn(arr4)); // true

? ? ? console.log("----------------------")


? ? ? // 第四種方法 數(shù)組方法 isArray()? Array.isArray(variable);? ? 最好用的一種

? ? ? var arr5 = [4,5,6]

? ? ? var obj3 = {name: '前端開發(fā)', age: '23'}

? ? ? console.log(Array.isArray(arr5))? //true

? ? ? console.log(Array.isArray(obj3)) //false

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容