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

JS中的四種數(shù)據(jù)類型判斷方法

 更新時間:2022年05月25日 16:45:56   作者:前端點線面來源  
js有多種數(shù)據(jù)類型(Number(數(shù)值)、String(字符串)、Boolean(布爾值)、Null、Undefined、Symbol、Object、函數(shù)等),在開發(fā)過程中難免需要判斷數(shù)據(jù)類型,本文總結(jié)了四種判斷方法給大家分享,需要的朋友可以參考一下

本文總結(jié)了四種判斷方法:

1、typeof

typeof是一個運算符,其有兩種使用方式:(1)typeof(表達式); (2)typeof 變量名;返回值是一個字符串,用來說明變量的數(shù)據(jù)類型;所以可以用此來判斷number, string, object, boolean, function, undefined, symbol 這七種類型,每種情況返回的內(nèi)容如下表所示:

// 字符串 
console.log(typeof('lili')); // string 
// 數(shù)字 
console.log(typeof(1)); // number 
// 布爾值 
console.log(typeof(true)); // boolean 
// undefined 
console.log(typeof(undefined)); // undefined 
// 對象 
console.log(typeof({})); // object 
// 數(shù)組 
console.log(typeof([])); // object 
// null 
console.log(typeof(null)); // object 
// 函數(shù) 
console.log(typeof(() => {})); // function 
// Symbol值 
console.log(typeof(Symbol())); // symbol 

2、instanceof

instanceof運算符用于檢測構(gòu)造函數(shù)的 prototype屬性是否出現(xiàn)在某個實例對象的原型鏈上,返回值為布爾值,用于指示一個變量是否屬于某個對象的實例。其語法如下所示:

object instanceof constructor 

const arr = [1, 2]; 
// 判斷Object的prototype有沒有在數(shù)組的原型鏈上 
console.log(arr instanceof Object); // true 
// 數(shù)組arr的原型 
const proto1 = Object.getPrototypeOf(arr); 
console.log(proto1); // [] 
// 數(shù)組arr的原型的原型 
const proto2 = Object.getPrototypeOf(proto1); 
console.log(proto2); // [] 
// Object的prototype 
console.log(Object.prototype); 
// 判斷arr的原型是否與Object的prototype相等 
console.log(proto1 === Object.prototype); // false 
// 判斷arr的原型的原型是否與Object的prototype相等 
console.log(proto2 === Object.prototype); // true 
 


3、constructor

該種判斷方式其實涉及到原型、構(gòu)造函數(shù)和實例之間的關(guān)系,更深層次的講解將放到后面的內(nèi)容,下面只需要簡單了解一下這三者關(guān)系即可。

在定義一個函數(shù)(構(gòu)造函數(shù))的時候,JS引擎會為其添加prototype原型,原型上有其對應(yīng)的constructor屬性指向該構(gòu)造函數(shù),從而原型和構(gòu)造函數(shù)之間互相知道對方。當(dāng)構(gòu)造函數(shù)實例化的時候,會產(chǎn)生對應(yīng)的實例,其實例可以訪問對應(yīng)原型上的constructor屬性,這樣該實例就可以了解到通過誰產(chǎn)生了自己,這樣就可以在新對象產(chǎn)生之后了解其數(shù)據(jù)類型。

const val1 = 1; 
console.log(val1.constructor); // [Function: Number] 
const val2 = 'abc'; 
console.log(val2.constructor); // [Function: String] 
const val3 = true; 
console.log(val3.constructor); // [Function: Boolean] 


雖然該方法可以判斷其數(shù)據(jù)類型,但存在兩個缺點:

  • null undefined 是無效的對象,因此是不會有 constructor 存在的,這兩種類型的數(shù)據(jù)需要通過其他方式來判斷。
  • 函數(shù)的 constructor 是不穩(wěn)定的,這個主要體現(xiàn)在自定義對象上,當(dāng)開發(fā)者重寫 prototype 后,原有的 constructor 引用會丟失,constructor 會默認為 Object

4、toString()

toString() Object 的原型方法,調(diào)用該方法,默認返回當(dāng)前對象的 [[Class]] 。這是一個內(nèi)部屬性,其格式為[object Xxx] ,其中 Xxx 就是對象的類型。所以利用Object.prototype.toString()方法可以對變量的類型進行比較準(zhǔn)確的判斷。

該類型針對不同不同變量的類型返回的結(jié)果如下所示:

利用該方法很容易構(gòu)建一個鑒型函數(shù),代碼如下所示:

function type(target) { 
    const ret = typeof(target); 
    const template = { 
        "[object Array]": "array",  
        "[object Object]":"object", 
        "[object Number]":"number - object", 
        "[object Boolean]":"boolean - object", 
        "[object String]":'string-object' 
    } 
    if(target === null) { 
        return 'null'; 
    } 
    else if(ret == "object"){ 
        const str = Object.prototype.toString.call(target); 
        return template[str]; 
    } 
    else{ 
        return ret; 
    } 
} 

console.log(type({})); // object 
console.log(type(123)); // number 
console.log(type('123')); // string 

到此這篇關(guān)于JS中的四種數(shù)據(jù)類型判斷方法的文章就介紹到這了,更多相關(guān)JS中的數(shù)據(jù)類型判斷方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論