判斷陣列的型別

2022-08-24 20:57:09 字數 4287 閱讀 5349

資料型別判斷之typeof

typeof可以解決大部分的資料型別判斷,是乙個一元運算,放在乙個運算值之前,其返回值為乙個字串,該字串說明運算數的型別,所以判斷某個是否為string型別,可以直接 if(typeof(你的值) == "string"){}

以下是各種資料型別返回結果:12

3456

78910

vara="string"; console.log(a);//string

vara=1; console.log(a);//number

vara=false; console.log(a);//boolean

vara; console.log(typeofa);//undfined

vara =null; console.log(typeofa);//object

vara = document; console.log(typeofa);//object

vara = ; console.log(a);//object

vara =function(){}; console.log(typeofa)//function 除了可以判斷資料型別還可以判斷function型別

這樣一來就很明顯了,除了前四個型別外,null、物件、陣列返回的都是object型別;

對於函式型別返回的則是function,再比如typeof(date),typeof(eval)等。

然後這裡就可以再引申出另乙個灰常熱門並且解決方法已普遍存在的問題,如何判斷資料是個陣列型別?

---------------------------------------其實這才是我的目的,咩~----------------------------------------------

js判斷陣列型別的方法

方法一之instanceof

instance,故名思義,例項,例子,所以instanceof 用於判斷乙個變數是否某個物件的例項,是乙個三目表示式---和typeof最實質上的區別

a instanceof b?alert("true"):alert("false")  //注意b值是你想要判斷的那種資料型別,不是乙個字串,比如array

舉個栗子:12

vara=;

console.log(ainstanceofarray)//返回true

方法二之constructor

在w3c定義中的定義:constructor 屬性返回對建立此物件的陣列函式的引用

就是返回物件相對應的建構函式。從定義上來說跟instanceof不太一致,但效果都是一樣的

如: (a instanceof array)   //a是否array的例項?true or false

(a.constructor == array)  // a例項所對應的建構函式是否為array? true or false

舉個栗子:12

3456

78functionemployee(name,job,born)

varbill=newemployee("bill gates","engineer",1985);

console.log(bill.constructor);//輸出function employee(name, jobtitle, born)

那麼判斷各種型別的方法就是:12

345console.log(.constructor == array);

console.log({}.constructor == object);

console.log("string".constructor == string);

console.log((123).constructor == number);

console.log(true.constructor == boolean);

-------------------------------------以下不是原創--------------------------------------

較為嚴謹並且通用的方法:12

34functionisarray(object)

!!注意:

使用instaceof和construcor,被判斷的array必須是在當前頁面宣告的!比如,乙個頁面(父頁面)有乙個框架,框架中引用了乙個頁面(子頁面),在子頁面中宣告了乙個array,並將其賦值給父頁面的乙個變數,這時判斷該變數,array == object.constructor;會返回false;

原因:1、array屬於引用型資料,在傳遞過程中,僅僅是引用位址的傳遞。

2、每個頁面的array原生物件所引用的位址是不一樣的,在子頁面宣告的array,所對應的建構函式,是子頁面的array物件;父頁面來進行判斷,使用的array並不等於子頁面的array;切記,不然很難跟蹤問題!

方法三之特性判斷法

以上方法均有一定的缺陷,但要相信人民大眾的智慧型是無所不能及的,我們可根據陣列的一些特性來判斷其型別12

3456

7functionisarray(object)

有length和splice並不一定是陣列,因為可以為物件新增屬性,而不能列舉length屬性,才是最重要的判斷因子。

ps: 在這裡普及下 propertyisenumerable 方法:

object. propertyisenumerable(proname)

判斷指定的屬性是否可列舉

備註:如果 proname 存在於 object 中且可以使用乙個 for…in 迴圈窮舉出來,那麼 propertyisenumerable 屬性返回 true。如果 object 不具有所指定的屬性或者所指定的屬性不是可列舉的,那麼 propertyisenumerable 屬性返回 false。

propertyisenumerable 屬性不考慮原型鏈中的物件。

示例:1

2vara =newarray(,"banana","cactus");

document.write(a.propertyisenumerable(1));

方法四之最簡單的方法12

3functionisarray(o)

怎樣判斷陣列型別

方法一 instance of方法 宣告變數 var arr new array var newarr var str var nul null var und undefined var obj new object var newobj 宣告函式 function isarray obj 呼叫函...

JS資料型別判斷和陣列型別判斷

js六大資料型別 number string object boolean null undefined string 由單引號或雙引號來說明,如 string number 什麼整數啊浮點數啊都叫數字,boolean 就是true和false啦 undefined 未定義,就是你建立乙個變數後卻沒...

js判斷變數的型別為陣列型別

js的陣列是無型別的 陣列元素可以是任意型別,並且同乙個陣列中的不同元素也可能有不同的型別。陣列的元素可以是物件或其他陣列,這樣就可以建立複雜的資料結構。通常我們可以用一元運算子typeof來判斷js的資料型別,但是對於陣列這樣乙個特殊的物件卻只能返回 object typeof可以解決大部分的資料...