2013-05-23 39 views
7

Tôi đang cố gắng xác định xem một giá trị thuộc tính đối tượng có "trung thực" thông qua một câu lệnh chuyển đổi hay không.Đánh giá Truthy trong câu lệnh switch

Sử dụng khối mẫu này:

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

kết thúc khai thác gỗ:

"no success in switch" 
"success in if" 

cách thích hợp để làm điều này là gì?

+0

Ý anh là gì bởi "truthy"? – James

+0

James: http://www.sitepoint.com/javascript-truthy-falsy/ giải thích các khái niệm về sự thật và sai. –

Trả lời

12

Bạn có thể làm điều này:

case !!test.foo: 

Điều này sẽ buộc một chuyển đổi để boolean.

+0

Mẹo hay. Tôi không biết rằng – FLX

+0

Quên trả lời lại điều này. Tôi quên mất số tiền phạt bằng ole. Cảm ơn bạn đã bồi dưỡng! –

-1

Ngoài việc sử dụng !! để ép buộc một boolean, bạn có thể làm điều này với báo cáo kết quả switch để đánh giá truthy/falsy:

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

Dưới đây là một tập hợp các chức năng và các xét nghiệm để bạn có thể nhìn thấy cho bản thân bạn:

(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]); 
    } 
}()); 

Và, tất nhiên :), các jsFiddle bắt buộc: http://jsfiddle.net/AE8MU/

+0

Cảm ơn bạn đã trả lời chi tiết. Tôi thích terser !!, nhưng đây là một giải pháp tốt đẹp quá. –

Các vấn đề liên quan