TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Two Things About Conditionals in JavaScript

13 点作者 thisisblurry超过 12 年前

4 条评论

modeless超过 12 年前
The last refactoring is not equivalent to the previous versions. The behavior of the function is changed when it is passed a non-number like an object reference or NaN (before it would return undefined; after it returns 'big').
Stratoscope超过 12 年前
Poor article with poor code, and poor responses from the author in the article's own comments. The comment from 'isaacs' is better thought out than the article itself.<p>As isaacs points out, the first part of the article about "else if" is much ado about very little. And why the sideways hint that there is something wrong with "else if"?<p>The second part is lazy and inconsistent refactoring. Why go through the exercise of removing the &#60;= 100 test and never notice that there's also a redundant &#60;= 10 test in all three examples?<p>If you know JavaScript at all, this article is a waste of your time.
评论 #4904504 未加载
emef超过 12 年前
I'd argue that it's a bad practice to leave out the last else if condition even when it seems redundant. What happens if NaN is passed to your function? It will return 'big' when you probably want to throw an exception or something more meaningful.
carstimon超过 12 年前
Under the if/else: I kind of prefer the first code sample, then the second, then the third. The first emphasizes to the reader the structure of the function. With one look you can see the function is choosing from three options.<p>For example, if the insides of the if statements were a touch more complicated, the third could also fit the pattern of a recursive function with two base cases. We would have to read more to figure out the idea.<p>The first is preferable to the second because I immediately see that only one thing will happen, I don't need to look inside the if's. (Relevant if the inside is more complicated).
评论 #4902860 未加载