8

我正在尝试通过 switch 语句确定对象属性值是否“真实”。

使用此示例块:

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

最终记录:

"no success in switch"
"success in if"

这样做的正确方法是什么?

4

2 回答 2

17

你可以这样做 :

case !!test.foo:

这将强制转换为布尔值。

于 2013-05-23T19:05:21.520 回答
0

除了用于!!强制布尔值之外,您还可以使用switch语句来评估真/假:

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

这是一组函数和测试,您可以自己查看:

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

而且,当然:),强制性的jsFiddle:http: //jsfiddle.net/AE8MU/

于 2013-05-24T13:46:26.297 回答