首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算switch语句中的Truthy

计算switch语句中的Truthy
EN

Stack Overflow用户
提问于 2013-05-24 03:03:40
回答 2查看 1.7K关注 0票数 8

我正在尝试通过switch语句来确定对象属性值是否为"truthy“。

使用此示例块:

代码语言:javascript
复制
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");
}

以日志记录结束:

代码语言:javascript
复制
"no success in switch"
"success in if"

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-05-24 03:05:22

您可以这样做:

代码语言:javascript
复制
case !!test.foo:

这将强制转换为布尔值。

票数 16
EN

Stack Overflow用户

发布于 2013-05-24 21:46:26

除了使用!!强制布尔值之外,您还可以使用switch语句执行此操作,以计算truthy/falsy:

代码语言:javascript
复制
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;
}

下面是一组函数和测试,你可以自己看看:

代码语言:javascript
复制
(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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16721912

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档