簡體   English   中英

具有邏輯運算符的復雜三元運算符

[英]complex ternary operator with logical operator

大家好,我正在調試一個很小的Java腳本插件,我似乎已經弄清楚了其中的大部分內容,但是在理解以下功能時遇到了一些困難:

CBPFWTabs.prototype._show = function( idx ) {
        if( this.current >= 0 ) {
            this.tabs[ this.current ].className = '';
            this.items[ this.current ].className = '';
        }
        // change current
        this.current = idx != undefined ? idx : this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0;
        this.tabs[ this.current ].className = 'tab-current';
        this.items[ this.current ].className = 'content-current';
    };

我開始運行該功能的每個部分,並將各個部分放在一起。 最終我非常成功,MDN的出色文檔也提供了幫助,但是,我仍然很難理解以下內容:

this.current = idx != undefined ? idx : this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0;

三元運算符和邏輯運算符的組合確實令人困惑,為簡化上述操作,我嘗試用普通英語閱讀,而我最好的解釋是:

如果idx 不等於 undefined並且idx 等於 this.current ...並且我對此很困惑,那么我什至無法猜測接下來會發生什么。 如果有人能用純正的語言來解釋那條線,那就太好了!

編輯:::我只是讀了下面的評論中的一篇非常好的文章鏈接,因此只想澄清一下,因為下面的代碼行(嵌套的三元運算符):

int i = 5;
string result = i % 2 == 0 ? "a" : i % 3 == 0 ? "b" : i % 5 == 0 ? "c" : i % 7 == 0 ? "d" : "e"; 

最后的e在開關情況下的功能更像默認情況,對嗎? 謝謝 。

亞歷山大。

您發布的是兩個(嵌套的)三元運算符:

this.current = idx != undefined ? idx : c;

...其中c是以下結果:

 (this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0);

換句話說,嵌套三元運算符被評估為第一三元運算符的false路徑。


如果仍然不清楚; 這樣想

if (idx != undefined) {
    this.current = idx;
} else if (this.options.start >= 0 && this.options.start <  this.items.length) {
    this.current = this.options.start;
} else {
    this.current = 0;
}

三元運算符a = cond ? b : c a = cond ? b : c可以解釋如下:

if (cond) {
    a = b;
} else {
    a = c;
}

如您所見,“ c”部分中有一個嵌套的三元運算符,因此,您可以在其中放置另一個嵌套的三元運算符,以幫助您將其翻譯為英語。 希望清楚。

您可以使用if編寫這樣的行:

if(idx != undefined) {this.current = idx
} else if (this.options.start >= 0 && this.options.start < this.items.length) {this.current = this.options.start
} else {this.current = 0}

將其分為以下幾部分:

this.current = idk != undefined ? idk : ...
如果idx不是undefinedthis.current等於idx

this.current = ... ? ... : this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : ...
否則,它會等於this.options.start如果this.options.start比大於或等於0,但較低的this.items.length

this.current = ... ? ... : ... && ... ? ... : 0
否則它將等於0。

盡量避免像這樣復雜的三元運算符,簡單的if很容易閱讀。

在這里使用普通if / else

if (idx != undefined) {
    this.current = idx;    
} else if (this.options.start >= 0 && this.options.start < this.items.length) {
    this.current = this.options.start
} else {
    this.current = 0;
}

似乎/然后/其他進行分配操作可能會有所幫助。 盡管您可以將其用於許多其他用途,但是通常建議不要將其用於復雜的if / else程序流程。 因為很難遵循幾行代碼。 一個當您應該感到舒適使用時的好例子就是..

var foo = amount > 5 ? 'greater than 5' : 'less than or equal to 5';
// amount === 7 --> foo is now set to 'greater than 5'
// amount === 4 --> foo is now set to 'less than or equal to 5'

只是一個顯示復雜度可以接受的示例,嵌套2也應該很好,但我會避免嵌套更多,即使2也會減慢家伙閱讀代碼的速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM