2017-10-26 9 views
1

に合格していないことができます。渡されたパラメータは、明示的な「未定義」か、私は、次の必要は

function(arg) { 
    if (undefined was passed explicitly) 
    //case 1 
    if (no parameter was passed) 
    // case 2 
} 

が可能ということですか? 次の文は、例の間で違いはありません。

typeof args === 'undefined' 
args === undefined 
args === null 
+0

あなたはそれはあなたがそれをやりたいということであるものの実施例を提供することはできますか? – Lixus

答えて

1

javascript rest or spread operator ...を使用すると、関数の引数を定義できます。

function call(...args){ 
 
if(args.length == 0) 
 
    { 
 
    console.log("no args passed"); 
 
    } 
 
    else if(args[0] == undefined || args[0] == 'undefined'){ 
 
    console.log("args is undefined"); 
 
    } 
 
    else 
 
    { 
 
    console.log(args); 
 
    } 
 
} 
 

 
call(undefined); 
 
call('undefined'); 
 
call(null) 
 
call(1,2,4); 
 
call();

1

が、私はこのような何かをしたい:

function test(...args) { 
    if (args.length && typeof args[0] === 'undefined') { 
    console.log('explicitly undefined'); 
    } else { 
    console.log('just undefined'); 
    } 
} 

広がりオペレータが配列に任意の渡される引数を拡大していきます。配列の長さがゼロの場合、何も渡されなかったので、undefinedが暗黙的です。 が配列長であるである場合、その型を明示的にチェックすることができます。

関連する問題