Declaring variables and functions, in what order?

梦想的初衷 提交于 2021-02-11 09:17:33

问题


Context

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

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

Example:

var example = {

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

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

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

}

Questions

  • Alphabetically is the best one ?
  • Is that the order can improve the speed of code execution ?

回答1:


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 suggests using something like:

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

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

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

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).




回答2:


I assume this is psuedo-code and not an actual ones. However, it's more a matter of code guidelines – readability, maintainability... – than other.

Also, I will consider to use multiple var statement to declare variables. I use one var statement only if it's just declaration and the variables are logically related each other.

In addition, because hoisting, it also doesn't matter to JavaScript in which point of the scope you're declaring a variables: JS always move the declaration on the top, so you're free to shape the code in logical blocks that helps you to maintain and read better your own code.




回答3:


I'm not sure sure what your question is but in your question declaring variables and functions, in what order? you mentioned the word order so here is something important when you declare variable and functions

test();
function test()
{
    alert(1);
}

The function called before it's declaration but it'll run and won't through any errors, but following will cause an error

a();
a = function()
{
    alert(1);
};

The function call should be called after assigning the the function expression to the variable. You can find more here MDN and Decent Programming Advice and JavaScript Hoisting Explained.




回答4:


You should declare variables and functions in the order that suits you best. For other details check http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml



来源:https://stackoverflow.com/questions/11491334/declaring-variables-and-functions-in-what-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!