如何缩短我的条件陈述 [英] How to shorten my conditional statements

查看:98
本文介绍了如何缩短我的条件陈述的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很长的条件语句,如下所示:

I have a very long conditional statement like the following:

if(test.type == 'itema' || test.type == 'itemb' || test.type == 'itemc' || test.type == 'itemd'){
    // do something.
}

我想知道我是否可以将这个表达式/语句重构为更简洁的形式。

I was wondering if I could refactor this expression/statement into a more concise form.

关于如何实现这个的任何想法?

Any idea on how to achieve this?

推荐答案

Put将您的值放入数组中,并检查您的项目是否在数组中:

Put your values into an array, and check if your item is in the array:

if ([1, 2, 3, 4].includes(test.type)) {
    // Do something
}

如果你支持的浏览器不具备的 数组#include 方法,你可以使用此polyfill

If a browser you support doesn't have the Array#includes method, you can use this polyfill.


更新:因为我们现在有包括方法,使用 hack没有意义了。只是为那些有兴趣知道它是如何工作和/或在其他代码中遇到它的人保留。

Update: Since we now have the includes method, there's no point in using the ~ hack anymore. Just keeping this here for people that are interested in knowing how it works and/or have encountered it in other's code.

而不是检查如果 indexOf 的结果是> = 0 ,那么有一个很好的小捷径:

Instead of checking if the result of indexOf is >= 0, there is a nice little shortcut:

if ( ~[1, 2, 3, 4].indexOf(test.type) ) {
    // Do something
}

这是小提琴: http://jsfiddle.net/HYJvK/

这是如何工作的?如果在数组中找到一个项目, indexOf 将返回其索引。如果找不到该项,它将返回 -1 。没有太多细节,按位NOT运算符,仅为 -1 0 $ c>。

How does this work? If an item is found in the array, indexOf returns its index. If the item was not found, it'll return -1. Without getting into too much detail, the ~ is a bitwise NOT operator, which will return 0 only for -1.

我喜欢使用的快捷方式,因为它比在返回时进行比较更简洁值。我希望JavaScript有一个 in_array 函数,它直接返回一个布尔值(类似于PHP),但这只是一厢情愿(更新:它现在确实如此)它被称为包括。见上文)。请注意,jQuery的 inArray 虽然共享PHP的方法签名,但实际上模仿了原生的 indexOf 功能(这在不同情况下很有用) ,如果指数是你真正追求的那个。)

I like using the ~ shortcut, since it's more succinct than doing a comparison on the return value. I wish JavaScript would have an in_array function that returns a Boolean directly (similar to PHP), but that's just wishful thinking (Update: it now does. It's called includes. See above). Note that jQuery's inArray, while sharing PHP's method signature, actually mimics the native indexOf functionality (which is useful in different cases, if the index is what you're truly after).

重要提示:使用代字号快捷方式似乎有争议,一些强烈认为代码不够清晰,应该不惜一切代价避免(参见本答复的评论)。如果你分享他们的情绪,你应该坚持 .indexOf(...)> = 0 解决方案。

Important note: Using the tilde shortcut seems to be swathed in controversy, as some vehemently believe that the code is not clear enough and should be avoided at all costs (see the comments on this answer). If you share their sentiment, you should stick to the .indexOf(...) >= 0 solution.

JavaScript中的整数是有符号的,这意味着最左边的位保留为标志位;一个标志,表示该数字是正数还是负数, 1 为负数。

Integers in JavaScript are signed, which means that the left-most bit is reserved as the sign bit; a flag to indicate whether the number is positive or negative, with a 1 being negative.

以下是一些示例在32位的二进制格式的正数:

Here are some sample positive numbers in 32-bit binary format:

1 :    00000000000000000000000000000001
2 :    00000000000000000000000000000010
3 :    00000000000000000000000000000011
15:    00000000000000000000000000001111

现在这里是相同的数字,但是否定:

Now here are those same numbers, but negative:

-1 :   11111111111111111111111111111111
-2 :   11111111111111111111111111111110
-3 :   11111111111111111111111111111101
-15:   11111111111111111111111111110001

为什么这些奇怪的组合为负数?简单。负数只是正数+ 1的倒数;将负数添加到正数应始终产生 0

Why such weird combinations for the negative numbers? Simple. A negative number is simply the inverse of the positive number + 1; adding the negative number to the positive number should always yield 0.

要理解这一点,让我们做一些简单的二进制算术。

To understand this, let's do some simple binary arithmetic.

以下是我们如何将 -1 添加到 +1

Here is how we would add -1 to +1:

   00000000000000000000000000000001      +1
+  11111111111111111111111111111111      -1
-------------------------------------------
=  00000000000000000000000000000000       0

以下是我们如何将 -15 添加到 15

And here is how we would add -15 to +15:

   00000000000000000000000000001111      +15
+  11111111111111111111111111110001      -15
--------------------------------------------
=  00000000000000000000000000000000        0

我们如何获得这些结果?通过定期添加,我们在学校教授的方式:从最右边的列开始,然后添加所有行。如果总和大于最大的单位数字(十进制的数字是 9 ,但二进制是 1 )我们将剩余部分带到下一列。

How do we get those results? By doing regular addition, the way we were taught in school: you start at the right-most column, and you add up all the rows. If the sum is greater than the greatest single-digit number (which in decimal is 9, but in binary is 1) we carry the remainder over to the next column.

现在,正如您将注意到的那样,在向正数添加负数时,最右侧的列是并非所有 0 s总是有两个 1 s,当它们加在一起时会产生 2 。两个二进制表示为 10 ,我们将 1 带到下一列,然后输入 0 表示第一列中的结果。左侧的所有其他列只有一行 1 ,因此从前一列继承的 1 将再加起来 2 ,然后继续......这个过程重复进行,直到我们到达最左边的列,其中 1 被遗留无处可去,所以它会溢出并迷失,我们只剩下 0

Now, as you'll notice, when adding a negative number to its positive number, the right-most column that is not all 0s will always have two 1s, which when added together will result in 2. The binary representation of two being 10, we carry the 1 to the next column, and put a 0 for the result in the first column. All other columns to the left have only one row with a 1, so the 1 carried over from the previous column will again add up to 2, which will then carry over... This process repeats itself till we get to the left-most column, where the 1 to be carried over has nowhere to go, so it overflows and gets lost, and we're left with 0s all across.

此系统称为 2的补充。你可以阅读更多关于这个位置:

This system is called 2's Complement. You can read more about this here:

2对签名整数的补充表示

2's Complement Representation for Signed Integers.

既然2的补码中的速成课程结束了,你会注意到 -1 是唯一一个二进制表示为 1 一直都是。

Now that the crash course in 2's complement is over, you'll notice that -1 is the only number whose binary representation is 1's all across.

使用按位NOT运算符,给定数字中的所有位被倒置了。从反转所有位获得 0 的唯一方法是,如果我们从 1 开始。

Using the ~ bitwise NOT operator, all the bits in a given number are inverted. The only way to get 0 back from inverting all the bits is if we started out with 1's all across.

所以,所有这些都是一种啰嗦的方式,说 ~n 只会返回 0 如果 n -1

So, all this was a long-winded way of saying that ~n will only return 0 if n is -1.

这篇关于如何缩短我的条件陈述的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆