2013-05-23 7 views
7

Ich versuche zu ermitteln, ob ein Objekteigenschaftswert über eine switch-Anweisung "truthy" ist.Auswerten von Truthy in switch-Anweisung

Mit diesem Probenblock:

var test = { 
    foo: "bar" 
} 

switch(true) { 
    case test.foo: 
    console.log("success in switch"); 
    break 
    default: 
    console.log("no success in switch"); 
    break 
} 

if (test.foo) { 
    console.log("success in if"); 
} else { 
    console.log("no success in if"); 
} 

Protokollierung endet:

"no success in switch" 
"success in if" 

Was ist der richtige Weg, dies zu tun?

+0

Was meinst du mit "truthy"? – James

+0

James: http://www.sitepoint.com/javascript-truthy-falsy/ erklärt die Konzepte von Truthy und Falsy. –

Antwort

12

Sie können dies tun:

case !!test.foo: 

Diese eine Umwandlung gezwungen würde, boolean.

+0

Schöner Tipp. Ich wusste das nicht – FLX

+0

Ich habe vergessen, darauf zu antworten. Ich vergaß das gute alte Doppel gleich. Danke für die Auffrischung! –

-1

Zusätzlich !! zur Verwendung einer boolean zu zwingen, können Sie dies mit dem switch Aussage truthy/falsy zu bewerten:

switch (true) {    // use a boolean to force case statement to evaluate conditionals 
case (val ? true : false): // force a truthy/falsy evaluation of val using parentheses and the ternary operator 
    console.log(val + ' evaluates as truthy in the switch statement.'); 
    break; 
default: 
    console.log(val + ' evaluates as falsy in the switch statement.'); 
    break; 
} 

Hier ist eine Reihe von Funktionen und Tests, so dass Sie für sich selbst sehen kann:

(function() { 
    'use strict'; 
    var truthitizeSwitch = function (val) { 
      switch (true) {    // use a boolean to force case statement to evaluate conditionals 
      case (val ? true : false): // force a truthy/falsy evaluation of val using parentheses and the ternary operator 
       console.log(val + ' evaluates as truthy in the switch statement.'); 
       break; 
      default: 
       console.log(val + ' evaluates as falsy in the switch statement.'); 
       break; 
      } 
      return !!val; // use !! to return a coerced boolean 
     }, 
     truthitizeIf = function (val) { 
      if (val) {  // if statement naturally forces a truthy/falsy evaluation 
       console.log(val + ' evaluates as truthy in the if statement.'); 
      } else { 
       console.log(val + ' evaluates as falsy in the if statement.'); 
      } 
      return !!val; // use !! to return a coerced boolean 
     }, 
     tests = [ 
      undefined,        // falsey: undefined 
      null,         // falsey: null 
      parseInt('NaNificate this string'),  // falsey: NaN 
      '',          // falsey: empty string 
      0,          // falsey: zero 
      false,         // falsey: boolean false 
      {},          // truthy: empty object 
      {"foo": "bar"},       // truthy: non-empty object 
      -1,          // truthy: negative non-zero number 
      'asdf',         // truthy: non-empty string 
      1,          // truthy: positive non-zero number 
      true         // truthy: boolean true 
     ], 
     i; 
    for (i = 0; i < tests.length; i += 1) { 
     truthitizeSwitch(tests[i]); 
     truthitizeIf(tests[i]); 
    } 
}()); 

Und natürlich :), die obligatorische jsFiddle: http://jsfiddle.net/AE8MU/

+0

Danke für die ausführliche Antwort pete. Ich bevorzuge den Tester, aber das ist auch eine schöne Lösung. –