js資料型別和陣列判斷

2021-07-22 03:48:30 字數 3554 閱讀 6751

js六大資料型別:number、string、object、boolean、null、undefined

string: 由單引號或雙引號來說明,如"string"

number:什麼整數啊浮點數啊都叫數字,***~

boolean: 就是true和false啦

undefined:未定義,就是你建立乙個變數後卻沒給它賦值~

null: 故名思久,null就是沒有,什麼也不表示

object: 這個我也很難解釋的說。就是除了上面五種之外的型別

--------------------上面的都是浮雲,下面的才是神馬------------------------------

資料型別判斷之 typeof

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

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

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

string

var a=1; console.log(a); //

number

var a=false; console.log(a); //

boolean

var a; console.log(typeof a); //

undfined

var a = null; console.log(typeof a); //

object

var a = document; console.log(typeof a); //

object

var a = ; console.log(a); //

object

var a = function(){}; console.log(typeof a) //

function 除了可以判斷資料型別還可以判斷function型別

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

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

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

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

js判斷陣列型別的方法

方法一之 instanceof

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

a instanceof b?alert("true"):alert("false") 

//注意b值是你想要判斷的那種資料型別,不是乙個字串,比如array

舉個栗子:

var a=;

console.log(a

instanceof array)//返回true

方法二之 constructor

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

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

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

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

舉個栗子:

function

employee(name,job,born)

var bill=new employee("bill gates","engineer",1985);

console.log(bill.constructor);

//輸出function employee(name, jobtitle, born)

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

console.log(.constructor ==array);

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

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

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

console.log(

true.constructor == boolean);

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

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

function

isarray(object)

!!注意:

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

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

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

方法三之 特性判斷法

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

function

isarray(object)

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

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

object. propertyisenumerable(proname)
判斷指定的屬性是否可列舉

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

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

示例:document.write(a.propertyisenumerable(1));

方法四之 最簡單的方法

對於這種方法,以下有幾個鏈結可供參考解釋:

function

isarray(o)

本文**於:

當然,你知道啦,這篇東西不完全是出自我手的啦,所以參考文章還是要列一列的啦:

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

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

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

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

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

隨筆記錄 js六大資料型別 number string object boolean null undefined 判斷資料型別 typeof typeof可以解決大部分的資料型別判斷,是乙個一元運算,放在乙個運算值之前,其返回值為乙個字串,該字串說明運算數的型別,所以判斷某個是否為string型別...