在JavaScript中使用'return'而不​​是'else' [英] Using 'return' instead of 'else' in JavaScript

查看:118
本文介绍了在JavaScript中使用'return'而不​​是'else'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要一些非常复杂的JavaScript处理的项目。这包括很多嵌套如果 - 其他 s在很多地方。我一般都照顾通过读取堆栈&NBSP其他技巧来优化JavaScript代码尽可能;溢出,但我想知道如果以下两个结构将使仅在速度方面有什么区别:

I am working on a project which requires some pretty intricate JavaScript processing. This includes a lot of nested if-elses in quite a few places. I have generally taken care to optimise JavaScript code as much as possible by reading the other tips on Stack Overflow, but I am wondering if the following two constructs would make any difference in terms of speed alone:

if(some_condition) {
    // process
    return ;
}

// Continue the else condition here

vs

if(some_condition) {
    // Process
}

else {
   // The 'else' condition...
}


推荐答案

我总是选择第一种方法。更容易阅读,减少缩进。就执行速度而言,这将取决于实现,但我希望它们都是相同的。

I always go with the first method. Easier to read, and less indentation. As far as execution speed, this will depend on the implementation, but I would expect them both to be identical.

这篇关于在JavaScript中使用'return'而不​​是'else'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆