以什么顺序声明变量和函数? [英] Declaring variables and functions, in what order?

查看:74
本文介绍了以什么顺序声明变量和函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在 一致,可读且可维护.

I'm learning how to code in javascript consistently, readably and maintainably.

我对变量和函数的声明顺序一无所知.

I found nothing about the order of declaration of variables and functions.

var example = {

    A: function() {
        var a, b, c;
    },

    B: function() {
        var a, b, c;
    },

    C: function() {
        var a, b, c;
    }

}

问题

  • 按字母顺序排列是最好的吗?
  • 该命令可以提高代码执行速度吗?
  • 推荐答案

    我使用 jslint 来检查代码质量.它可以与Visual Studio以及很多其他东西集成在一起,这确实很棒.

    I use jslint to check the code quality. It can be integrated with Visual Studio and a lot of other stuff which is really nice.

    JSLint建议使用类似的内容:

    JSLint suggests using something like:

    var example = {
        A: function () {
            var a, b, c;
        },
    
        B: function () {
            var a, b, c;
        },
    
        C: function () {
            var a, b, c;
        }
    };
    

    关于变量,建议始终在封闭范围的开头声明它们,因为这实际上就是代码的解释方式(这就是JavaScript语义).

    Regarding the variables, it suggests declaring them always at the start of the enclosing scope, since that's actually how the code will be interpreted (that's the JavaScript semantic).

    关于性能,您无法通过更改顺序来提高或降低性能.

    Regarding performance, you cannot improve or decrease performance by changing the order.

    关于顺序...您应该按照对您(和您的团队)更有意义的顺序进行操作.我个人喜欢自上而下或自上而下地进行操作(这意味着首先将最重要的功能放在首位,然后将从属功能放在那之后,依此类推...或者反之亦然...先将较简单的功能放在首位,然后然后在这些功能之上构建的功能.)

    Regarding the order... You should do it in the order which has more sense to you (and your team). I personally like doing top-down or bottom-top (That means putting the most important functions first, and then put the dependent functions after that one, etc... or the other way around... Put the simpler functions first, and then the functions that build on top of those ones).

    这篇关于以什么顺序声明变量和函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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