亚洲精品久久久久久第一页-人妻少妇精彩视品一区二区三区-91国产自拍免费视频-免费一级a在线播放视频正片-少妇天天日天天射天天爽-国产大屁股喷水视频在线观看-操美女骚穴抽插性爱视频-亚洲 欧美 中文字幕 丝袜-成人免费无码片在线观看

js判斷不等于null和空對象 js判斷不為空不為0


js判斷不等于null和空對象 js判斷不為空不為0

文章插圖
js中常用的數(shù)據(jù)類型有:
值類型(基本類型):字符串(String)、數(shù)字(Number)、布爾(Boolean)、對空(Null)、未定義(Undefined)、Symbol 。
引用數(shù)據(jù)類型:對象(Object)、數(shù)組(Array)、函數(shù)(Function) 。
就以上數(shù)據(jù)類型先舉幾個(gè)例子:
// 字符串(String)let a='word';// 數(shù)字(Number)let b=123;// 布爾(Boolean)let c=true;// 對空(Null)let d=null;// 未定義(Undefined)let e;// Symbollet f=Symbol('123');// 對象(Object)let g={name:'xiangming'};// 數(shù)組(Array)let h=[1,2,3,4,5];// 函數(shù)(Function)let i=function () {return 'done';};1、最常見的判斷方法:typeof
console.log('a:'+typeof a);// a:stringconsole.log('b:'+typeof b); // b:numberconsole.log('c:'+typeof c); // c:booleanconsole.log('d:'+typeof d);// d:objectconsole.log('e:'+typeof e);// e:undefinedconsole.log('f:'+typeof f);// f:symbolconsole.log('g:'+typeof g);// g:objectconsole.log('h:'+typeof h);// h:objectconsole.log('i:'+typeof i);// i:function所以,typeof 在判斷除Object類型的對象時(shí)比較方便 。
2、判斷已知對象類型的方法:instanceof
console.log('a is string:'+a instanceof String);// a is string:trueconsole.log('b is number:'+b instanceof Number);// b is number:trueconsole.log('b is number:'+b instanceof number);// Uncaught ReferenceError: number is not defined注意:instanceof 后面一定要是對象類型,并且大小寫不能錯(cuò),該方法適合一些條件選擇或分支 。
3、通用但很繁瑣的方法:prototype(推薦)
// 定義方法獲取變量類型function var_type(data) {return Object.prototype.toString.call(data).replace(/^\[object\s(.+)\]$/, '$1').toLowerCase();}console.log('a:'+var_type(a));// a:stringconsole.log('b:'+var_type(b));// b:numberconsole.log('c:'+var_type(c));// c:booleanconsole.log('d:'+var_type(d));// d:nullconsole.log('e:'+var_type(e));// e:undefinedconsole.log('f:'+var_type(f));// f:symbolconsole.log('g:'+var_type(g));// g:objectconsole.log('h:'+var_type(h));// h:arrayconsole.log('i:'+var_type(i));// i:function此方法雖稍顯繁瑣,但只需簡單封裝一個(gè)方法即可 。
文章最后給大家推薦一個(gè)小編自己寫的通用后臺框架云靜Admin通用后臺,小編也是第一次嘗試開源代碼,還望大家不吝賜教 。


    以上關(guān)于本文的內(nèi)容,僅作參考!溫馨提示:如遇健康、疾病相關(guān)的問題,請您及時(shí)就醫(yī)或請專業(yè)人士給予相關(guān)指導(dǎo)!

    「愛刨根生活網(wǎng)」www.malaban59.cn小編還為您精選了以下內(nèi)容,希望對您有所幫助: