What is the best-practice casing style for javascript? Why?

前端 未结 6 1920
梦毁少年i
梦毁少年i 2020-12-09 09:19

One aspect of javascript that it\'s hard to find information on is casing practices. By casing practices, I mean what casing style (ie. camel-case, pascal-case, etc) should

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 09:50

    What I have seen so far is a very large diversity of casing standards.

    As far as I'm concerned, I use C# styling for writing my JavaScript code. I use classes a lot (well functions as classes, and usually don't have independent functions.) So, I use PascalCase for class names, public methods, properties and all global variables and camelCase for arguments, local variables and private functions. This somehow reflects my common environment, helping to distinguish the variable scopes. I also tend to keep my class functions in a separate file with the same name as my ClassName (ClassName.js, ClassName.min.js).

    This was about my approach.

    I also noticed that Java programmers, follow the Java rules (and the writing style resembles the Java language.) Ruby on Rails programmers follow their own naming standards such as underscore_separated_var_name.

    Further, as you mentioned, there is a tendency to use pascalCase a lot in naming in very popular frameworks whose authors come from different communities like Linux/Open source community and Microsoft developers (jQuery, knockout.js, JSJaC, etc.)

    I should note that none of these methods are wrong or right, when it comes to JS. The primary purpose of your naming conventions and file structuring is the readability. If you are consistent then you in future and your fellow developers will quickly understand and get on with your code.

提交回复
热议问题