Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
351 views
in Technique[技术] by (71.8m points)

javascript - problem using lint in switch - case with ternary operator

I am new in using "lint"

case 'up':
   index > 0 ? index-- : 0; break;

I have a pre-commit lint script, which shows the following error: "error Expected an assignment or function call and instead saw an expression"

If I change the code to the long version everything is okay?

case 'up':
    if (index > 0) {
       index--;
    } else {
       index = 0;
    }

Isn't the fist code snippet not the short form of the second?

Thanks in advance


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

No, you need an assignment to index:

index = index > 0 ? index - 1 : 0;

If index is always positive, you chould shorten it a bit to

index = index ? index - 1 : 0;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...