var num = 123;var str = 'abcdef';var bool = true;var arr = [1, 2, 3, 4];var json = {name:'wenzi', age:25};var func = function(){ console.log('this is function'); }var und = undefined;var nul = null;var date = new Date();var reg = /^[a-zA-Z]{5,20}$/;var error= new Error();console.log(typeof num,typeof str,typeof bool,typeof arr,typeof json,typeof func,typeof und,typeof nul,typeof date,typeof reg,typeof error);試下看看。
判斷js中的數據類(lèi)型有一下幾種方法:typeof、instanceof、constructor、prototype、$.type()/jquery.type(),接下來(lái)主要比較一下這幾種方法的異同。
1、最常見(jiàn)的判斷方法:typeof:
2、判斷已知對象類(lèi)型的方法: instanceof:
3、根據對象的constructor判斷: constructor:
4、通用但很繁瑣的方法: prototype:
5、無(wú)敵萬(wàn)能的方法:jquery.type():
通常情況下用typeof 判斷就可以了,遇到預知Object類(lèi)型的情況可以選用instanceof或constructor方法,實(shí)在沒(méi)轍就使用$.type()方法。
去百度文庫,查看完整內容>內容來(lái)自用戶(hù):張臣js類(lèi)型識別是一個(gè)簡(jiǎn)單而又糾結的問(wèn)題。
typeof操作符可以說(shuō)是js中最簡(jiǎn)單直接的類(lèi)型判斷方式了,缺點(diǎn)就是有時(shí)也不太靠譜。typeof[];// -> 'object'typeofnull;// -> 'object'它無(wú)法識別Array類(lèi)型和null類(lèi)型,還有Date、Math、Error、JSON、RegExp這些類(lèi)型就更不必多說(shuō)了。
instanceof操作符能判斷一個(gè)對象是否是某個(gè)類(lèi)(或子類(lèi))的一個(gè)實(shí)例。vararr=[1,2,3];arrinstanceofArray;// -> ;// -> true因為Array是Object的子類(lèi)相比typeof,instanceof的范圍廣泛很多,還可以判斷自定義類(lèi)型。
如:functionAnimal(){}varcat=newAnimal();catinstanceofAnimal;// -> true對于string、boolean、number等原始值類(lèi)型,instanceof就不太好用了:3instanceofNumber;// -> ;// -> false'abc'instanceofString;// -> false因為原始值類(lèi)型并不是任何類(lèi)的實(shí)例,還記得以前提到過(guò)的String()以及new String()的區別吧。constructor能反映創(chuàng )建當前對象的構造函數,也沒(méi)有如instanceof那樣無(wú)法正確識別string等原始值類(lèi)型:(3).constructor===Number// -> truetrue.constructor===Boolean// -> true'abc'.constructor===String// -> true因為對于string、boolean、number等原始值類(lèi)型在訪(fǎng)問(wèn)其屬性的時(shí)候,會(huì )先創(chuàng )建對應的數據類(lèi)型的臨時(shí)實(shí)例,訪(fǎng)問(wèn)的是這個(gè)臨時(shí)實(shí)例的方法。
判斷js中的數據類(lèi)型有一下幾種方法:typeof、instanceof、constructor、prototype、$.type()/jquery.type(),接下來(lái)主要比較一下這幾種方法的異同。
1、最常見(jiàn)的判斷方法:typeof: 2、判斷已知對象類(lèi)型的方法: instanceof: 3、根據對象的constructor判斷: constructor: 4、通用但很繁瑣的方法: prototype: 5、無(wú)敵萬(wàn)能的方法:jquery.type(): 通常情況下用typeof 判斷就可以了,遇到預知Object類(lèi)型的情況可以選用instanceof或constructor方法,實(shí)在沒(méi)轍就使用$.type()方法。
可以參考下面的兩種方法:
1、直接判斷對象不為null
if (!myObj) {
var myObj = { };
}
Javascript語(yǔ)言是"先解析,后運行",解析時(shí)就已經(jīng)完成了變量聲明
2、使用window對象判斷某對象是否存在
if (!window.myObj) {
var myObj = { };
}
擴展資料:
javascript函數
charCodeAt(index)返回一個(gè)整數,該整數表現String對象中指定位置處的字符的Unicode編碼
concat(string2)銜接兩條或少條字符串
fromCharCode(num1, num2, …,BB霜, numN)獲取指定的Unicode值并返回字符串
indexOf(searchString, startIndex) 返回字符串中第一個(gè)呈現指定字符串的地位
lastlndexOf(searchString, startIndex) 返回字符串中最后一個(gè)呈現指定字符串的地位
match(regex) 在字符串中查覓指定值
參考資料來(lái)源:百度百科-javascript
參考資料來(lái)源:百度百科-JavaScript 函數
如何判斷js中的數據類(lèi)型:typeof、instanceof、constructor、prototype方法比較如何判斷js中的類(lèi)型呢,先舉幾個(gè)例子:var a = "iamstring.";var b = 222;var c= [1,2,3];var d = new Date();var e =function(){alert(111);};var f =function(){this.name="22";};最常見(jiàn)的判斷方法:typeofalert(typeof a) ------------> stringalert(typeof b) ------------> numberalert(typeof c) ------------> objectalert(typeof d) ------------> objectalert(typeof e) ------------> functionalert(typeof f) ------------> function其中typeof返回的類(lèi)型都是字符串形式,需注意,例如:alert(typeof a == "string")-------------> truealert(typeof a == String)---------------> false另外typeof可以判斷function的類(lèi)型;在判斷除Object類(lèi)型的對象時(shí)比較方便。
判斷已知對象類(lèi)型的方法: instanceofalert(c instanceof Array)---------------> truealert(d instanceofDate) alert(f instanceof Function)------------> truealert(f instanceof function)------------> false注意:instanceof后面一定要是對象類(lèi)型,并且大小寫(xiě)不能錯,該方法適合一些條件選擇或分支。根據對象的constructor判斷:constructoralert(c.constructor ===Array) ----------> truealert(d.constructor === Date)-----------> truealert(e.constructor ===Function) -------> true注意: constructor 在類(lèi)繼承時(shí)會(huì )出錯eg,function A(){};function B(){};A.prototype = new B(); //A繼承自Bvar aObj = new A();alert(aobj.constructor === B) ----------->true;alert(aobj.constructor === A) ----------->false;而instanceof方法不會(huì )出現該問(wèn)題,對象直接繼承和間接繼承的都會(huì )報true:alert(aobj instanceof B) ---------------->true;alert(aobj instanceof B) ---------------->true;言歸正傳,解決construtor的問(wèn)題通常是讓對象的constructor手動(dòng)指向自己:aobj.constructor = A;//將自己的類(lèi)賦值給對象的constructor屬性alert(aobj.constructor === A) ----------->true;alert(aobj.constructor === B) ----------->false; //基類(lèi)不會(huì )報true了;通用但很繁瑣的方法: prototypealert(Object.prototype.toString.call(a) === '[object String]')-------> true;alert(Object.prototype.toString.call(b) === '[object Number]')-------> true;alert(Object.prototype.toString.call(c) === '[object Array]')-------> true;alert(Object.prototype.toString.call(d) === '[object Date]')-------> true;alert(Object.prototype.toString.call(e) === '[object Function]')-------> true;alert(Object.prototype.toString.call(f) === '[object Function]')-------> true;大小寫(xiě)不能寫(xiě)錯,比較麻煩,但勝在通用。
通常情況下用typeof判斷就可以了,遇到預知Object類(lèi)型的情況可以選用instanceof或constructor方法,簡(jiǎn)單總結下,挖個(gè)坑,歡迎補充。
alert(typeof a) ------------> string
alert(typeof b) ------------> number
alert(typeof c) ------------> object
alert(typeof d) ------------> object
alert(typeof e) ------------> function
alert(typeof f) ------------> function
其中typeof返回的類(lèi)型都是字符串形式,需注意,例如:
alert(typeof a == "string") -------------> true
alert(typeof a == String) ---------------> false
另外typeof 可以判斷function的類(lèi)型;在判斷除Object類(lèi)型的對象時(shí)比較方便。
聲明:本網(wǎng)站尊重并保護知識產(chǎn)權,根據《信息網(wǎng)絡(luò )傳播權保護條例》,如果我們轉載的作品侵犯了您的權利,請在一個(gè)月內通知我們,我們會(huì )及時(shí)刪除。
蜀ICP備2020033479號-4 Copyright ? 2016 學(xué)習?shū)B(niǎo). 頁(yè)面生成時(shí)間:2.976秒