js中各種數(shù)據(jù)類型檢測和判定的實戰(zhàn)示例
介紹
在前端開發(fā)中,js 有各種各樣的數(shù)據(jù)類型,數(shù)據(jù)類型檢測是每個開發(fā)者都必須掌握基礎(chǔ)知識。數(shù)據(jù)類型檢測的方法也有很多種,本題將封裝一個通用的數(shù)據(jù)類型檢測函數(shù)。
準(zhǔn)備
本題已經(jīng)內(nèi)置了初始代碼,打開實驗環(huán)境,目錄結(jié)構(gòu)如下:
.
└── main.js
其中:
main.js 是待補充代碼的 js 文件。
在 main.js 文件中封裝好方法以后,可以嘗試在該文件中手動調(diào)用已經(jīng)封裝好的方法,通過命令 node main.js 來執(zhí)行文件,打印對應(yīng)的日志。
目標(biāo)
完善 main.js 中 getType 函數(shù)中的 TODO 部分,返回傳入值的對應(yīng)數(shù)據(jù)類型。
傳入值以及 getType 函數(shù)返回值(大小寫可忽略)按照如下方式對應(yīng):
傳入值 返回值
‘s’ string
0 number
| 傳入值 | 返回值 |
|---|---|
| false | boolean |
| undefined | undefined |
| Symbol() | symbol |
| function fn(){} | function |
| 123n | bigint |
| null | null |
| {} | object |
| [] | array |
| new Date | date |
| new Map | map |
| new Set | set |
| /a/ | regexp |
測試用例
// 示例
const testArr = [
"s",
0,
false,
undefined,
Symbol(),
function () {},
123n,
null,
{},
[],
new Date(),
new Map(),
new Set(),
/a/,
];
const result = testArr.map((item) => getType(item));
console.log("得到的結(jié)果:", result);
/*
得到的結(jié)果:
[
'string', 'number',
'boolean', 'undefined',
'symbol', 'function',
'bigint', 'null',
'object', 'array',
'date', 'map',
'set', 'regExp'
]
*/
值得注意的是需要我們區(qū)別typeof與instanceof的用法。instanceof運算符用來判斷一個構(gòu)造函數(shù)的prototype屬性所指向的對象是否存在另外一個要檢測對象的原型鏈上。關(guān)于原型鏈請自行學(xué)習(xí),這里不過多解釋。typeof返回的是一個字符串,具體用法請見代碼。typeof只能檢測出圖中的8種類型。并且你如果使用typeof檢測 null、Date、RegExp、Error都會返回為object類型。所以最好使用instanceof檢測

代碼
/**
* @description: 數(shù)據(jù)類型檢測
* @param {*} data 傳入的待檢測數(shù)據(jù)
* @return {*} 返回數(shù)據(jù)類型
*/
function getType(data) {
// TODO:待補充代碼
if(typeof data=="String"){
return 'string';
}else if( typeof data=="number"){
return "number"
}else if(typeof data== "boolean"){
return "boolean"
}else if(typeof data=="undefined"){
return "undefined"
}else if(typeof data== "function"){
return "function"
}else if(typeof data=== "symbol"){
return "symbol"
}
else if(typeof data== "bigint"){
return "bigint"
}else if( data=== null){
return "null"
}else if(Array.isArray(data)){
return "array"
}else if(data instanceof Date){
return "date"
}else if(data instanceof RegExp){
return "regExp"
}
else if(data instanceof Map){
return "map"
}else if(data instanceof Set){
return "set"
}
else if(typeof data=="string"){
return "string"
}else if(typeof data=="object"){
return "object"
}
}
const testArr = [
"s",
0,
false,
undefined,
Symbol(),
function () {},
123n,
null,
{},
[],
new Date(),
new Map(),
new Set(),
/a/,
];
const result = testArr.map((item) => getType(item));
console.log("得到的結(jié)果:", result);
module.exports = {
getType
}
使用
可以把代碼復(fù)制到一個js文件中。在終端運行node 文件名.js即可。測試結(jié)果如下,結(jié)果是從左往右開始讀,然后下一行

總結(jié)
到此這篇關(guān)于js中各種數(shù)據(jù)類型檢測和判定的文章就介紹到這了,更多相關(guān)js數(shù)據(jù)類型檢測和判定內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- JS中檢測數(shù)據(jù)類型的幾種方式及優(yōu)缺點小結(jié)
- 淺談javascript的數(shù)據(jù)類型檢測
- 關(guān)于JS數(shù)據(jù)類型檢測的多種方式總結(jié)
- js數(shù)據(jù)類型檢測總結(jié)
- JavaScript中檢測數(shù)據(jù)類型的四種方法
- javascript基本數(shù)據(jù)類型及類型檢測常用方法小結(jié)
- 在javaScript中檢測數(shù)據(jù)類型的幾種方式小結(jié)
- JavaScript數(shù)據(jù)類型檢測代碼分享
- js學(xué)習(xí)總結(jié)_基于數(shù)據(jù)類型檢測的四種方式(必看)
- JS數(shù)組索引檢測中的數(shù)據(jù)類型問題詳解
相關(guān)文章
Bootstrap輪播插件中圖片變形的終極解決方案 使用jqthumb.js
這篇文章主要介紹了Bootstrap輪播插件中圖片變形的終極解決方案,使用jqthumb.js,感興趣的小伙伴們可以參考一下2016-07-07
用 js 的 selection range 操作選擇區(qū)域內(nèi)容和圖片
本篇文章主要介紹了用js的selection range操作選擇區(qū)域內(nèi)容和圖片的相關(guān)知識。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04
table對象中的insertRow與deleteRow使用示例
本文為大家介紹下table對象insertRow deleteRow的使用示例,適合新手朋友們2014-01-01

