JS關於資料型別檢測的幾種方式

2021-08-22 07:14:16 字數 4250 閱讀 6646

js中我們只用乙個var就能定義所有型別的變數,非常方便,但是也同樣給我們造成了困擾,如果我們想知道乙個函式的返回值是什麼型別的,或者輸入的資訊是什麼型別的時候就要通過對資料進行檢測,所以我們該如何進行資料型別的檢測呢?

資料型別檢測方式:

typeof

typeof:用來檢測資料型別的運算子;使用方式是 typeof + 檢測的內容。

使用typeof檢測資料型別,首先返回的都是乙個字串,字串中包含對應的資料型別;

var num  = 2;

console.log(typeof num); // ->控制台輸出字串number

console.log(typeof

typeof

typeof

typeof

function

() {});

// 輸出的結果為字串string,因為第一次使用typeof檢測後結果為乙個字串資料型別的資料,以後每次檢測都是string

typeof的侷限性

1. typeof null 的結果為「object」

2. 不能具體細分是陣列還是正則,還是物件中的其他值,因為使用typeof檢測資料型別,對於物件資料型別中左右的值,最後返回的結果都是「object」

instanceof

instanceof:用來檢測某個例項是不是屬於某個類;使用方法: 例項 instanceof 類名

instanceof的侷限性

1. 不能用來處理字面量建立出來的基本型別值:對於基本的資料型別來說,字面量方式建立出來的結果和例項方式建立出來的結果是有一定的區別的,從嚴格的意義上來講,只有例項建立出來的結果才是標準物件資料型別的值,也是標準的number這乙個類的例項;對於字面量方式建立的結果是基本的資料型別值,不是嚴謹的例項,但是由於js的鬆散性,導致可以使用number.prototype上提供的方法

console.log(1

instanceof

number);//->控制台輸出false

console.log(new

number(1) instanceof

number);//-> 控制台輸出true

instanceof只要在當前例項的原型鏈上,我們檢測出來的結果都為true

在類的原型鏈繼承當中,我們最後檢測出來的結果未必正確

function

fn()

var ary = new

array;

fn.prototype = ary;//原型繼承:讓子類的原型等於父類的乙個例項

var f =new fn;

// f->fn.prototype->array.prototype->object.prototype

console.log(f instanceof

array); //-> true

constructor:建構函式

constructor:建構函式 這種方法與instanceof非常相似

var obj = ;

console.log(obj.constructor === array ); //->true

console.log(obj.constructor === regexp); //->false

//console還可以出來基本的資料型別

var num = 1;

console.log(num.constructor === number);//->true

// constructor檢測和instanceof一同,一般情況下是檢測不了的

var reg = /^$/;

console.log(reg.constructor === regexp);//-> true

console.log(reg.constructor === regexp);//-> false

侷限性:我們可以把類的原型進行重寫,在重寫的過程中很有可能出現把之前的constructor覆蓋了,這樣檢測出來的結果就是不準確的;對於特殊的資料型別null和undefined,它們的所屬類是null和undefined,但是瀏覽器吧這兩個類保護起來了,不允許我們進行訪問使用

function

fn()

fn.prototype = new

array;

var f =new fn;

console.log(f.constructor);//-> array

object.prototype.tostring.call()

這種方法是我們真是專案中最長用的也是現在最準確的一種方式

首先獲取object原型上的tostring方法,讓方法執行,並且改變方法中的this關鍵字的指向

在了解這種方式之前我們先了解下tostring這個方法

tostring:從字面上來看是轉化成字串,但是某些tostring方法不僅僅是轉化成字串;對於number、string、boolean、array、regexp、date、function原型上的tostring方法都是①把當前的資料型別轉化為字串的型別(它們的作用僅僅只是用來轉成字串);但是在object原型上的tostring方法不同於這些,②它的作用是返回當前方法的執行主體(方法中的this)所屬的類的詳細資訊。

第一中型別轉成字串

方法是轉化為字串  

console.log((1).tostring()); //->這裡的tostring是number.prototype.tostring用法是轉成字串-> '1'

console.log((1).__proto__.__proto__.tostring());//[object object]

console.log((128).tostring(2/8/10));//把數字轉化為二進位制、八進位制、十進位制

object原型上的

().tostring();

console.log(obj.tostring());//-> tostring中的this是obj,返回的是obj所屬類的資訊->[object object]第乙個object代表當前例項是物件資料型別的(這個是固定死的),第二個object代表的是obj所屬的類是object

math.tostring();//->tostring中的this是math,返回的是math所屬類的資訊 -> [object math]

console.log(object.prototype.tostring.call()); //->[object array]

console.log(object.prototype.tostring.call(/^$/)); //->[object array]

console.log(({}).tostring.call(new

date)); //->[object date]

console.log(object.prototype.tostring.call(1)); //->[object number]

console.log(object.prototype.tostring.call('程式設計')); //->[object string]

console.log(object.prototype.tostring.call(null)); //->[object null]

console.log(object.prototype.tostring.call(undefined)); //->[object undefined]

console.log(object.prototype.tostring.call(function

() {})); //->[object function]

所有經過對比我們第四中方式的準確性是最高的,所以也是我們專案中經常使用的。are you get it!!!

JS中檢測資料型別的幾種方式

判斷js中的資料型別有一下幾種方法 typeof instanceof constructor prototype type jquery.type 接下來主要比較一下這幾種方法的異同。var a iamstring.var b 222 var c 1 2,3 var d newdate vare ...

資料型別檢測的幾種方式

1.typeof 缺點 對null和array等型別的檢測不是很方便 typeof null object typeof object 2.instanceof 缺點 1.只適用於物件型別 2.只要當前的這個類在例項的原型鏈上,檢測出來的結果都是true 123 instanceof number ...

js判斷資料型別的幾種方式

資料型別分為 基礎資料型別和引用 物件 資料型別 基礎資料型別 number string boolean undefined null 常見的引用資料型別 funciton object array 第一種,當資料型別是undefined null 時,可以用 來進行判斷 undefined un...