2013-05-23 24 views
7

Bir anahtar özellik ifadesi aracılığıyla bir nesne özellik değerinin "doğru" olup olmadığını belirlemeye çalışıyorum. Anahtar deyiminde Doğruluk değerlerini değerlendirmek

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"); 
} 

günlüğü biter

:

"no success in switch" 
"success in if" 

Bunu yapmanın doğru yolu nedir

bu örnek bloğu kullanarak?

+0

siz "truthy" ile ne demek istiyorsunuz? – James

+0

James: http://www.sitepoint.com/javascript-truthy-falsy/ doğruluk ve falsi kavramlarını açıklar. –

cevap

12

Bunu yapabilirsin:

case !!test.foo: 

Bu boolean bir dönüşüm zorlayacaktır.

+0

Güzel bahşiş. Ben bunu bilmiyordum – FLX

+0

Bu yanıtı geri almak. İyi ole çift eşitliği unuttum. Tazeleme için teşekkürler! –

-1

bir boolean zorlamayı !! kullanmanın yanı sıra, truthy/falsy değerlendirmek switch deyimi ile yapabilirsiniz: Eğer kendini görebilmesi Burada

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; 
} 

işlevleri ve testlerin bir dizi var: tabii
(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]); 
    } 
}()); 

Ve :), zorunlu jsFiddle: http://jsfiddle.net/AE8MU/

+0

Detaylı cevap pete için teşekkürler. Ben terser tercih ederim, ama bu da güzel bir çözüm. –

İlgili konular