课程: JavaScript: Best Practices for Data

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Use ternary statements judiciously

Use ternary statements judiciously

- My code for this video using a couple ternary statements to choose between values based on the truth value of a condition. On line seven, I'm setting a Boolean value for a variable based on the value of a property of my object. On line nine, I'm choosing the text to be logged based on whether I have custom text to display. Turnery statements are meant to serve as a quick substitute for larger logic patterns when you only need to make a quick comparison. They're more compact, which means you have the classic trade off of fewer characters at the cost of denser code that can be harder for a human to parse. Because of the potential draw backs of ternary statements, it's important to use them only when necessary. ESLint has a rule that flags ternaries that could easily be replaced. It also has a rule that lets you flag nested ternaries for elimination. Nested ternaries make that short, hard-to-parse code even more complex and to…

内容