除非与如果在 Javascript 中使用感叹号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28120525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unless vs if in Javascript with use of exclamation point?
提问by Henry
The code below attempts to print out "is even" for numbers divisible by 2.
下面的代码尝试打印可被 2 整除的数字的“偶数”。
Should it not be if (test) then() rather than: if (!test) then(), when condition tested is "n % 2". The code below seems to read "IF numbers are NOT divisible by 2, print out 'number is even' ", which does not seem logical.
应该不是 if (test) then() 而不是:if (!test) then(),当测试的条件是“n % 2”时。下面的代码似乎是“如果数字不能被 2 整除,打印出‘数字是偶数’”,这似乎不合逻辑。
More generally speaking, what are the advantages of writing an Unless function over using an If statement in specifying condition, when what we could do is simply write if (!condition)?
更一般地说,在指定条件时,如果我们可以做的只是编写 if (!condition),那么编写除非函数比使用 If 语句有什么优势?
Any help is much appreciated.
任何帮助深表感谢。
function unless(test, then) {
if (!test) then();
}
function repeat(times, body) {
for (var i = 0; i < times; i++) body(i);
}
repeat(5, function(n) {
unless(n % 2, function() {
console.log(n, "is even");
});
// → 0 is even
// → 2 is even
// → 4 is even
回答by Ixrec
The arguable advantage of this is that the code reads slightly more like English: "Unless n modulo 2 is nonzero, log to the console that n is even."
有争议的优点是代码读起来更像英语:“除非 n 模 2 非零,否则记录到控制台 n 是偶数。”
In my experience, the practical consequence of this is that most programmers will have to go double-check what unless() actually does before they feel comfortable with it. Because it's not a standard piece of Javascript, they have no way of knowing whether it uses ! or ==true or ===0 or some other ever-so-slightly different test, unless they go look at the implementation.
根据我的经验,这样做的实际后果是,大多数程序员在对它感到满意之前必须仔细检查除非()实际上做了什么。因为它不是标准的 Javascript,他们无法知道它是否使用 ! 或 ==true 或 ===0 或其他一些非常不同的测试,除非他们去看看实现。
My favorite example of this principle is COBOL. That language tried very hard to resemble English, so that even non-programmers could use it...but in reality both programmers and non-programmers seem to hate working with it.
我最喜欢的这个原则的例子是 COBOL。这种语言非常努力地模仿英语,因此即使是非程序员也可以使用它……但实际上,程序员和非程序员似乎都讨厌使用它。
回答by Jon
There are no advantages if the condition is provided inline as in this scenario. You can easily apply a transformation to negate it as appropriate in all cases (e.g. adding a !in front), and taking unlessout of the picture means there's one less thing the reader has to know about.
如果在这种情况下内联提供条件,则没有任何优势。在所有情况下,您都可以轻松地应用转换以将其取反(例如,!在前面添加一个),并且去掉unless图片意味着读者需要了解的事情少了一件。
There couldbe an advantage if the condition was provided in the form of a callback, e.g.:
有可能是一个优势,如果条件是在一个回调,例如形式提供:
function test() { return true; }
function unless(test, then) { if (!test()) then(); }
unless(test, function() { console.log("test failed"); });
In this situation you could not directly pass the negation of testto a hypothetical function onlyIfthat is complementary to unless, so having both onlyIfand unlesscan make the code more readable because it allows you to do this:
在这种情况下,您无法直接将 的否定传递给与互补test的假设函数,因此同时拥有和可以使代码更具可读性,因为它允许您执行以下操作:onlyIfunlessonlyIfunless
onlyIf(test, function() { console.log("test passed"); });
instead of this:
而不是这个:
onlyIf(function() { return !test(); }, function() { console.log("test passed"); });
The above situation could be even worse if the callback is given arguments that need to be propagated into test.
如果给回调提供需要传播到test.

