1. What the best way to check array or object with JQuery undefined + null?
I check array like that:
function f1(arr){
开发者_如何学运维if(arr!==undefined&&arr!=null){
//code
}
}
Is Jquery have better way?
2. What the best way to check String with JQuery trim(str)!==""?
I check String like that:
function f2(str){
if(str!==undefined&&str!=null&&$.trim(str)!==''){
//code
}
}
Is Jquery have better way?
Thanks
if ( $.isArray( arr ) ) { alert('is array'); }
if ( $.trim(str) != '' ) { alert('is non empty string'); }
Testing:
$.isArray({})
false
$.isArray('')
false
$.isArray(null)
false
$.trim(null)
""
$.trim( undefined )
""
EDIT: You can probably be more explicit in test #2 if you use typeof
.
if ( (typeof str === 'string') && ($.trim(str ) != '') ) {
}
精彩评论