简体   繁体   English

如何缩短这个条件?

[英]How can this conditional be shortened?

if (!this.initialized || (typeof this.activeId !== 'undefined' && options.qualification_id !== this.activeId)) {
    //Stuff
}

I want to execute code if the this.initialized flag is false or this.activeId value isn't the same as options.qualification_id .如果this.initialized标志为 false 或this.activeId值与options.qualification_id不同,我想执行代码。 I have to check if this.activeId is defined or not because Javascript will return true in the case of undefined !== this.activeId .我必须检查this.activeId是否已定义,因为在undefined !== this.activeId的情况下,Javascript 将返回 true。 The above is the conditional I've come up with, but I hate how long it is.以上是我提出的条件,但我讨厌它有多长时间。 What would be the shortest way to write it?写它的最短方法是什么?

如果this.activeId在定义时的有效值都是真值(例如它不能是0falsenull ),您可以使用:

if (!this.initialized || this.activeId && options.qualification_id != this.activeId)

Let's see what you can do to shorten this让我们看看你能做些什么来缩短它

(!this.initialized || (typeof this.activeId !== 'undefined' && options.qualification_id !== this.activeId))

Well first, you can remove the inner parenthesis.首先,您可以删除内括号。 According to the operator precedence table, logical AND has a "higher precedence" than logial OR.根据运算符优先级表,逻辑与比逻辑或具有“更高的优先级”。 In other words the parenthesis will be added for you by the interpreter.换句话说,口译员将为您添加括号。 So, your condition becomes:所以,你的情况变成:

(!this.initialized || typeof this.activeId !== 'undefined' && options.qualification_id !== this.activeId)

Not much better.好不了多少。 Another thing you can do is take advantage of JS type conversion.您可以做的另一件事是利用 JS 类型转换。 undefined values are falsey (are evaluated to false ), your condition can become: undefined值是假的(被评估为false ),您的条件可能变为:

(!this.initialized || this.activeId && options.qualification_id !== this.activeId)

However, this may insert a silent bug since an activeId of 0 will also be evaluated to false .但是,这可能会插入一个无声的错误,因为0activeId也将被评估为false You can solves this using the in operator which checks to see if the property exists on the object or on one of its prototypes (To make sure the property doesn't come from one of its prototypes you can use the hasOwnProperty method instead).您可以使用in运算符解决此问题,该运算符检查该属性是否存在于对象或其原​​型之一上(要确保该属性不是来自其原型之一,您可以改用hasOwnProperty方法)。

(!this.initialized || 'activeId' in this && options.qualification_id !== this.activeId).

Now to be honest, this doesn't look so much better to me.现在说实话,这对我来说并没有好多少。 As a rule, whenever the line becomes too long, just split it in multiple lines to increase readability.通常,当一行变得太长时,只需将其拆分为多行以增加可读性。

var qualificationIsDifferent = options.qualification_id !== this.activeId;
var isNotActive = this.hasOwnProperty('activeId') && qualificationIsDifferent;
var shouldDoStuff = !this.initialized || isNotActive;

if ( shouldDoStuff ) { ... }

Note that by using the above approach you lose some of the benefits of the short-circuit evaluation .请注意,通过使用上述方法,您将失去短路评估的一些好处。 It really depends on your goal, sometimes more verbose means more readable.这真的取决于你的目标,有时更详细意味着更易读。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM