版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)
文檔簡介
第javaScript中一些常見的數(shù)據(jù)類型檢查校驗?zāi)夸浨把猿R姷膸追N數(shù)據(jù)校驗方式typeof操作符instanceofconstructorcallapplyOtotype.toString結(jié)合Ftotype.callapply其他校驗數(shù)據(jù)類型的方法:總結(jié)源碼地址
前言
在JavaScript中,數(shù)據(jù)類型分為兩大類,一種是基礎(chǔ)數(shù)據(jù)類型,另一種則是復(fù)雜數(shù)據(jù)類型,又叫引用數(shù)據(jù)類型
基礎(chǔ)數(shù)據(jù)類型:數(shù)字Number字符串String布爾BooleanNullUndefinedSymbolsBigInt引用數(shù)據(jù)類型:日期Dete,對象Object,數(shù)組Array,方法Function,正則regex,帶鍵的集合:Maps,Sets,WeakMaps,WeakSets
基礎(chǔ)數(shù)據(jù)類型和引用數(shù)據(jù)類型的區(qū)別,在之前深拷貝的文章中提到過,這里不做詳細贅述。
傳送門:javaScript中深拷貝和淺拷貝簡單梳理
常見的幾種數(shù)據(jù)校驗方式
接下來會針對下面幾種數(shù)據(jù)類型,進行校驗
//基本數(shù)據(jù)類型
letstr="abc";
letnum=123;
letboo=true;
letundef=undefined;
lettestNull=null;
letsymb=Symbol("user");
letbigInt=BigInt(9007199254740999);
//復(fù)雜-引用數(shù)據(jù)類型
letarr=[1,2,3,4];
letfunc=function(){};
letobj={};
letdate1=newDate();
letsetObj1=newSet();
letsetObj2=newSet([1,2,3]);
letmapObj=newMap();
typeof操作符
typeof操作符,會返回一個字符串,表示未經(jīng)計算的操作數(shù)的類型
/**
*typeof操作符
*返回一個字符串,表示未經(jīng)計算的操作數(shù)的類型。
**/
console.log(typeofstr);//string
console.log(typeofnum);//number
console.log(typeofboo);//boolean
console.log(typeofundef);//undefined
console.log(typeoftestNull);//object
console.log(typeofsymb);//symbol
console.log(typeofbigInt);//bigint
console.log(typeofObject(bigInt));//object
console.log(typeofarr);//object
console.log(typeoffunc);//function
console.log(typeofobj);//object
console.log(typeofdate1);//object
console.log(typeofsetObj1);//object
console.log(typeofsetObj2);//object
console.log(typeofmapObj);//object
小結(jié)
使用typeof操作符的時候,我們可以看到一些較為特殊的情況:
null,數(shù)組array,set,map返回的是對象object
instanceof
instanceof用于檢測構(gòu)造函數(shù)的prototype屬性是否出現(xiàn)在某個實例對象的原型鏈上。
/**
*instanceof
*用于檢測構(gòu)造函數(shù)的prototype屬性是否出現(xiàn)在某個實例對象的原型鏈上。
**/
console.log(strinstanceofString);//false
console.log(newString("abc")instanceofString);//true
console.log(numinstanceofNumber);//false
console.log(newNumber(123)instanceofNumber);//true
console.log(booinstanceofBoolean);//false
console.log(newBoolean(true)instanceofBoolean);//false
console.log(undefinstanceofundefined);
//UncaughtTypeError:Right-handsideof'instanceof'isnotanobject
console.log(testNullinstanceofnull);
//UncaughtTypeError:Right-handsideof'instanceof'isnotanobject
console.log(symbinstanceofSymbol);//false
//Symbol不是構(gòu)造函數(shù),沒有new操作符
console.log(bigIntinstanceofBigInt);//false
console.log(Object(BigInt("22"))instanceofObject);//true
console.log(Object(BigInt("22"))instanceofBigInt);//true
console.log(arrinstanceofArray);//true
console.log(arrinstanceofObject);//true
console.log(funcinstanceofFunction);//true
console.log(funcinstanceofObject);//true
console.log(objinstanceofObject);//true
console.log(objinstanceofFunction);//false
console.log(nullinstanceofObject);//false
console.log(date1instanceofObject);//true
console.log(setObj1instanceofObject);//true
console.log(setObj2instanceofObject);//true
console.log(mapObjinstanceofObject);//true
console.log(setObj1instanceofArray);//false
console.log(setObj2instanceofArray);//false
console.log(mapObjinstanceofArray);//false
constructor
/**
*constructor
*返回創(chuàng)建實例對象的構(gòu)造函數(shù)的引用。
*注意,此屬性的值是對函數(shù)本身的引用,而不是一個包含函數(shù)名稱的字符串
*構(gòu)造函數(shù).prototype.constructor()
**/
//基本數(shù)據(jù)類型
letstr="abc";
letnum=123;
letboo=true;
letundef=undefined;
lettestNull=null;
letsymb=Symbol("user");
letbigInt=BigInt(9007199254740999);
//復(fù)雜-引用數(shù)據(jù)類型
letarr=[1,2,3,4];
letfunc=function(){};
letobj={};
letdate1=newDate();
functionconstructorFn(){
="11";
letcon1=newconstructorFn();
letsetObj1=newSet();
letsetObj2=newSet([1,2,3]);
letmapObj=newMap();
console.log(str.constructor);//String
console.log(num.constructor);//Number
console.log(boo.constructor);//Boolean
//console.log(testUndefined.constructor);//Cannotreadproperty'constructor'ofundefined
//console.log(testNull.constructor);//Cannotreadproperty'constructor'ofnull
console.log(symb.constructor);//Symbol
console.log(bigInt.constructor);//BigInt
console.log(arr.constructor);//Array
console.log(func.constructor);//Function
console.log(obj.constructor);//Object
console.log(date1.constructor);//Date
console.log(constructorFn.constructor);//Function
console.log(con1.constructor);//constructorFn
console.log(setObj1.constructor);//Set
console.log(setObj2.constructor);//Set
console.log(mapObj.constructor);//Map
*構(gòu)造函數(shù)校驗
**/
console.log(Function.constructor);//Function
console.log(Object.constructor);//Function
console.log(Array.constructor);//Function
console.log(Date.constructor);//Function
Ototype.toString.callOtotype.toString.apply
Ototype.toString()
在使用Ototype.toString.call或者Ototype.toString.apply檢查數(shù)據(jù)類型之前,我們先了解一下Ototype.toString和JavaScript中的構(gòu)造函數(shù)Function的原型方法apply和call:
/**
*返回一個表示該對象的字符串
*Ototype.toString()
*每個對象都有一個toString()方法,當(dāng)該對象被表示為一個文本值時,或者一個對象以預(yù)期的字符串方式引用時自動調(diào)用。
*默認情況下,toString()方法被每個Object對象繼承。
*如果此方法在自定義對象中未被覆蓋,toString()返回"[objecttype]",其中type是對象的類型。以下代碼說明了這一點:
**/
letisObj={name:"zhangsan"};
letisBoolean=true;
letisNumber=newNumber(123);
letisString="abc";
letisFun=newFunction();
console.log(isObj.toString());//[objectObject]
console.log(isBoolean.toString());//true
console.log(isNumber.toString());//123
console.log(isString.toString());//abc
console.log(newDate().toString());//ThuApr28202516:37:19GMT+0800(中國標(biāo)準(zhǔn)時間)
console.log(isFun.toString());//functionanonymous(){}
callapply
/**
*call()使用一個指定的this值和單獨給出的一個或多個參數(shù)來調(diào)用一個函數(shù),function.call(thisArg,arg1,arg2,...)
*apply()使用一個指定的this值和單獨給出的一個或多個參數(shù)來調(diào)用一個函數(shù),unc.apply(thisArg,[argsArray])
**/
//call基本使用;
functiona(){
console.log(this);
functionb(){
console.log(this);
a.call(b);//functionb(){}
b.call(a);//functiona(){}
call和apply最簡單的例子表明了,改變了當(dāng)前方法的this指向
同時這兩個方法的區(qū)別在于傳參的方式
Ototype.toString結(jié)合Ftotype.callapply
/**
*使用toString()檢測對象類型可以通過toString()來獲取每個對象的類型。
*為了每個對象都能通過Ototype.toString()來檢測,
*需要以Ftotype.call()或者Ftotype.apply()的形式來調(diào)用,傳遞要檢查的對象作為第一個參數(shù),稱為thisArg。
*那么Ototype.toString相當(dāng)于原生構(gòu)造函數(shù)的實例化對象isNumber,傳參數(shù)給Ototype.toString執(zhí)行
*實際上相當(dāng)于toString.call(new***);
**/
letstr="abc";
letnum=123;
letboo=true;
letundef=undefined;
lettestNull=null;
letsymb=Symbol("user");
letbigInt=BigInt(9007199254740999);
//復(fù)雜-引用數(shù)據(jù)類型
letarr=[1,2,3,4];
letfunc=function(){};
letobj={};
letdate1=newDate();
functiontestFun(){}
letnewTest=newtestFun();
letnewFun=newFunction();
letsetObj1=newSet();
letsetObj2=newSet([1,2,3]);
letmapObj=newMap();
console.log(Ototype.toString.apply(newString("sss")));//[objectString]
console.log(Ototype.toString.apply(str));//[objectString]
console.log(Ototype.toString.call(num));//[objectNumber]
console.log(Ototype.toString.call(boo));//[objectBoolean]
console.log(Ototype.toString.call(undef));//[objectUndefined]
console.log(Ototype.toString.call(testNull));//[objectNull]
console.log(Ototype.toString.call(symb));//[objectSymbol]
console.log(Ototype.toString.call(Object(bigInt)));//[objectBigInt]
console.log(Ototype.toString.call(bigInt));//[objectBigInt]
console.log(Ototype.toString.apply(arr));//[objectArray]
console.log(Ototype.toString.call(func));//[objectFunction]
console.log(Ototype.toString.call(obj));//[objectObject]
console.log(Ototype.toString.call(date1));//[objectDate]
console.log(Ototype.toString.call(testFun));//[objectFunction]
console.log(Ototype.toString.call(newTest));//[objectObject]
console.log(Ototype.toString.call(newFun));
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 小紅書合同協(xié)議書
- 店鋪延續(xù)合同范本
- 工程派遣協(xié)議書
- 資產(chǎn)贈予協(xié)議書
- 小孩入戶協(xié)議書
- 裝訂合作協(xié)議書
- 幼師招聘協(xié)議書
- 內(nèi)褲供應(yīng)合同范本
- 農(nóng)業(yè)投資合同范本
- 藥店出兌協(xié)議書
- 掃床護理課件
- 酒廠合作協(xié)議書合同
- 污泥干化項目施工組織設(shè)計
- 空氣能熱泵中央熱水系統(tǒng)調(diào)試
- JJF2085-2023低頻角加速度臺校準(zhǔn)規(guī)范
- 《校園欺凌現(xiàn)象與學(xué)校社會工作干預(yù)的探索》14000字論文
- 微積分(I)知到智慧樹章節(jié)測試課后答案2024年秋南昌大學(xué)
- AQ 1050-2008 保護層開采技術(shù)規(guī)范(正式版)
- MOOC 大數(shù)據(jù)與法律檢索-湖南師范大學(xué) 中國大學(xué)慕課答案
- JTS180-2-2011 運河通航標(biāo)準(zhǔn)
- 肺癌健康教育宣教
評論
0/150
提交評論