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