JavaScript空判定
5804 ワード
JavaScriptでは、空の判断が一般的ですが、空の判断はどのように正しく使うべきですか?データの種類によって判断方法が異なりますので、同一に論ずることはできません.前例のない判断においては、まずデータの種類を確定します.
1、異なるタイプの判定方法があります.
未曽有の予判データの種類において、希望のタイプが不明であれば、誤った判断や考えが生じる可能性があります.
番号付け
テストの種類
テストコード
結果
1
パラメータなしまたはundefinedに入る
test 1()
入力値:undefined、タイプ:undefined、戻り値:false
test 1(undefined、'undefined')
入力値:undefined、タイプ:undefined、戻り値:false
2
数字
test 1(0、'数字0')
入力値:数字0、タイプ:number、戻り値:false
test 1(-1、'数字-1')
入力値:数字-1、タイプ:number、戻り値:true
test 1(NaN、'デジタルNaN')
入力値:数字NaN、タイプ:number、戻り値:false
3
文字列
test 1('','文字列(内容なし)')
入力値:文字列(内容なし)、タイプ:string、戻り値:false
test 1('0'、'文字列0')
入力値:文字列0、タイプ:string、戻り値:true
test 1('','文字列スペース')
入力値:文字列スペース、タイプ:string、戻り値:true
4
オブジェクト
test 1({}、'空のオブジェクト({}};
入力値:空のオブジェクト({}タイプ:object,戻り値:true
test 1([],'空配列')
入力値:空配列、タイプ:object、戻り値:true
test 1(null、'null')
入力値:null、タイプ:object、戻り値:false
2、一般的な判定空関数:
番号付け
テストの種類
関数
テストコード
結果
1
文字列判定が空です
//***文字列は、すべてのスペース*/function trim(a)を削除します. if(typeof a='string'){ return a.replace(/\s+/') }else{ return a; } }/** *文字列判定空*/function isEmpty(a){ var b=trim(a) if((typeof a)=='string' && b){ return false; }else{ return true; } }
println(isEmpty(');println(isEmpty();println(isEmpty(');println(isEmpty);
true true true false
2
数字が空です
//****数字は空と判定して、もし数字のタイプではないならば、まずタイプの転換を行うべきです*/function isNull(a){ if((typeof a)='number'&a!=NaN){ return false; }else{ return true; } }
printl(isNull);printl(isNull);println(isNull);println(isNull(');println(isNull);printl(isNull(');println(isNull('9');println(isNull(1);
true true true true true true true true
3
総合的に空を判定し、対象としない場合
//****はタイプに関係なく、着信値を対象とした場合を考慮しない*/function isNull Empty(a){ if((typeof a)=='string') return isEmpty(a) } else if((typeof a)='number'&a!=NaN){ return false; }else{ return true; } }
println(isNull Empty);println(isNull Empty(null);println(isNull Empty(');println(isNull Empty);println(isNull Empty);println(isnull Empty);
true true true true true false true
4
オブジェクトの空判定
//****対象判定空*/function isemptyObj(a){ if((typeof a)=='object'&a!=null){ return false; }else{ return true; } }
println(isEmptyObj(null);println(isEmptyObj();println(isemptyObj);
true true true true
5
配列判定空
/****配列は空と判定され、配列は対象の一つである*/function isemptyAray(a){ if((typeof a)==object'&a!=null&&a.length>0){ return false; }else{ return true; } }
printl(isEmptyAray);printl(isEmptyAray);println(isemptyAray);printl(isemptyAray);
true true false
3、テストコード
1、異なるタイプの判定方法があります.
未曽有の予判データの種類において、希望のタイプが不明であれば、誤った判断や考えが生じる可能性があります.
番号付け
テストの種類
テストコード
結果
1
パラメータなしまたはundefinedに入る
test 1()
入力値:undefined、タイプ:undefined、戻り値:false
test 1(undefined、'undefined')
入力値:undefined、タイプ:undefined、戻り値:false
2
数字
test 1(0、'数字0')
入力値:数字0、タイプ:number、戻り値:false
test 1(-1、'数字-1')
入力値:数字-1、タイプ:number、戻り値:true
test 1(NaN、'デジタルNaN')
入力値:数字NaN、タイプ:number、戻り値:false
3
文字列
test 1('','文字列(内容なし)')
入力値:文字列(内容なし)、タイプ:string、戻り値:false
test 1('0'、'文字列0')
入力値:文字列0、タイプ:string、戻り値:true
test 1('','文字列スペース')
入力値:文字列スペース、タイプ:string、戻り値:true
4
オブジェクト
test 1({}、'空のオブジェクト({}};
入力値:空のオブジェクト({}タイプ:object,戻り値:true
test 1([],'空配列')
入力値:空配列、タイプ:object、戻り値:true
test 1(null、'null')
入力値:null、タイプ:object、戻り値:false
2、一般的な判定空関数:
番号付け
テストの種類
関数
テストコード
結果
1
文字列判定が空です
//***文字列は、すべてのスペース*/function trim(a)を削除します. if(typeof a='string'){ return a.replace(/\s+/') }else{ return a; } }/** *文字列判定空*/function isEmpty(a){ var b=trim(a) if((typeof a)=='string' && b){ return false; }else{ return true; } }
println(isEmpty(');println(isEmpty();println(isEmpty(');println(isEmpty);
true true true false
2
数字が空です
//****数字は空と判定して、もし数字のタイプではないならば、まずタイプの転換を行うべきです*/function isNull(a){ if((typeof a)='number'&a!=NaN){ return false; }else{ return true; } }
printl(isNull);printl(isNull);println(isNull);println(isNull(');println(isNull);printl(isNull(');println(isNull('9');println(isNull(1);
true true true true true true true true
3
総合的に空を判定し、対象としない場合
//****はタイプに関係なく、着信値を対象とした場合を考慮しない*/function isNull Empty(a){ if((typeof a)=='string') return isEmpty(a) } else if((typeof a)='number'&a!=NaN){ return false; }else{ return true; } }
println(isNull Empty);println(isNull Empty(null);println(isNull Empty(');println(isNull Empty);println(isNull Empty);println(isnull Empty);
true true true true true false true
4
オブジェクトの空判定
//****対象判定空*/function isemptyObj(a){ if((typeof a)=='object'&a!=null){ return false; }else{ return true; } }
println(isEmptyObj(null);println(isEmptyObj();println(isemptyObj);
true true true true
5
配列判定空
/****配列は空と判定され、配列は対象の一つである*/function isemptyAray(a){ if((typeof a)==object'&a!=null&&a.length>0){ return false; }else{ return true; } }
printl(isEmptyAray);printl(isEmptyAray);println(isemptyAray);printl(isemptyAray);
true true false
3、テストコード
function println(str){
document.writeln(str + '<br>');
}
function test1(a,desc){
var str = ' :' + desc + ', :'+ (typeof a);
if(a){
println(str + ', :<span style="color:#ff0000;">true</span>');
}else {
println(str + ', :<span style="color:#000000;">false</span>');
}
}
/**
*
*/
function trim(a){
if(typeof a =='string'){
return a.replace(/\s+/,'');
}else {
return a;
}
}
/**
*
*/
function isEmpty(a){
var b = trim(a);
if((typeof a) == 'string' && b){
return false;
}else {
return true;
}
}
/**
* , ,
*/
function isNull(a){
if((typeof a) == 'number' && a !=NaN){
return false;
}else {
return true;
}
}
/**
* ,
*/
function isEmptyArray(a){
if((typeof a) == 'object' && a!=null && a.length>0){
return false;
}else {
return true;
}
}
/**
*
*/
function isEmptyObj(a){
if((typeof a) == 'object' && a!=null){
return false;
}else {
return true;
}
}
/**
* ,
*/
function isNullEmpty(a){
if((typeof a) == 'string'){
return isEmpty(a);
} else if((typeof a) == 'number' && a !=NaN){
return false;
}else {
return true;
}
}
println('-------- undefined--------------');
test1();
test1(undefined, 'undefined');
println('------------------ ----------------------');
test1(0, ' 0');
test1(-1, ' -1');
test1(NaN, ' NaN');
println('------------------- -------------------');
test1('', ' ( )');
test1('0', ' 0');
test1(' ',' ');
println('------------------- --------------------');
test1({},' ({})');
test1([],' ');
test1(null, 'null');
println('----------------- ----------------');
println(isEmpty(' '));
println(isEmpty());
println(isEmpty(''));
println(isEmpty(' 1 '));
println('---------------- -------------------');
println(isNull());
println(isNull(null));
println(isNull(''));
println(isNull(' '));
println(isNull('9'));
println(isNull([1]));
println('---------------- --------------------');
println(isEmptyObj(null));
println(isEmptyObj());
println(isEmptyObj(undefined));
println('---------------- , --------------------');
println(isNullEmpty());
println(isNullEmpty(null));
println(isNullEmpty(''));
println(isNullEmpty(' '));
println(isNullEmpty('9'));
println(isNullEmpty([1]));
println('---------------- --------------------');
println(isEmptyArray(null));
println(isEmptyArray([]));
println(isEmptyArray([1,'2',{test:'ss'}]));
println('---------------- --------------------');
println([1,'2',{test:'ss'}]);