Javascript 带逻辑算子的复三值算子

Javascript 带逻辑算子的复三值算子,javascript,Javascript,嘿,伙计们,我正在调试一个非常小的java脚本插件,我似乎已经了解了其中的大部分内容,但我在理解以下功能时遇到了一些困难: CBPFWTabs.prototype._show = function( idx ) { if( this.current >= 0 ) { this.tabs[ this.current ].className = ''; this.items[ this.current ].className =

嘿,伙计们,我正在调试一个非常小的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';
    };
最后的
e
功能更像是开关箱中的默认情况,对吗。 多谢各位

亚历山大

您发布的是两个(嵌套的)三元运算符:

this.current = idx != undefined ? idx : c;
。。。其中,
c
是以下因素的结果:

 (this.options.start >= 0 && this.options.start < this.items.length ? this.options.start : 0);
您发布的是两个(嵌套的)三元运算符:

this.current = idx != undefined ? idx : c;
。。。其中,
c
是以下因素的结果:

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

三元运算符
a=cond?b:c
可以解释为:

if (cond) {
    a = b;
} else {
    a = c;
}
如您所见,“c”部分中有一个嵌套的三元运算符,因此您可以在其中放置另一个嵌套的if,以帮助您将其翻译为英语。
希望这能说明问题。

三元运算符
a=cond?b:c
可以解释为:

if (cond) {
    a = b;
} else {
    a = c;
}
如您所见,“c”部分中有一个嵌套的三元运算符,因此您可以在其中放置另一个嵌套的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}
如果(idx!=未定义){this.current=idx
}如果(this.options.start>=0&&this.options.start
您可以使用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}
如果(idx!=未定义){this.current=idx
}如果(this.options.start>=0&&this.options.start
将其分解为几个部分:

this.current=idk!=未定义?idk:…

如果
idx
不是
未定义的,则当前
等于
idx

this.current=…?…:this.options.start>=0&&this.options.start

否则它将等于
this.options.start
如果
this.options.start
大于或等于0,但小于
this.items.length

this.current=…?…:…&&&……?…:0

否则它将等于0


尽量避免这样复杂的三元运算符,如果
更容易阅读,则使用简单的

将其分解为几个部分:

this.current=idk!=未定义?idk:…

如果
idx
不是
未定义的,则当前
等于
idx

this.current=…?…:this.options.start>=0&&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;
}

这只是一个显示可接受复杂度的示例,nest 2也应该可以,但我会避免嵌套更多,即使是2也会减慢读取代码的速度。

这里使用的是简单的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;
}

这只是一个显示可接受复杂程度的示例,nest 2也应该可以,但我会避免嵌套更多,即使是2也会减慢读取代码的速度。

关于如何读取和格式化嵌套的ternaries有一个简单的方法。@ArtyomNeustroev谢谢你,dude,我看看这是否有帮助。这里有一个关于如何读取和格式化嵌套三元表的问题。@ArtyomNeustroev谢谢你,伙计,我看看这是否有帮助。谢谢你的回答,我刚刚添加了一个附加问题。@AlexanderSolonik:很高兴它有帮助。你的补充问题的答案是“是的”,但我不建议使用如此复杂的三元运算符。。。将其分解为
if/else
语句更容易,也更容易理解。不幸的是,当代java脚本围绕着这种语法。谢谢你的回答,我刚刚补充了一个问题。@AlexanderSolonik:很高兴这有帮助。你的补充问题的答案是“是的”,但我不建议使用如此复杂的三元运算符。。。将其分解为
if/else
语句更容易,也更容易理解。不幸的是,当代java脚本围绕着这种语法。nvm,泰。