JS Naming Standards
Naming standards are necessary to enhance the readability of the code. You can understand the meaning at a glance. Standards are in compliance with Rules make the code conducive to later maintenance and can also greatly improve development efficiency. A normal website has a lot of JS code. If you don’t follow certain rules during the writing process, you won’t be able to understand what you wrote later. This is a very troublesome thing, so you should write it in your usual time. During the practice, develop good coding habits.
Generally Hungarian nomenclature or camel case nomenclature is used.
The principle of Hungarian nomenclature: variable name = attribute + type + object description. His key is: prefix with one or more lowercase letters, followed by one or more words with an initial capital letter, which specifies the purpose of the variable.
The principle of camel case naming: the first word starts with a lowercase letter, and the first letter of each subsequent word is capitalized. For example: myFirstName, myLastName. Such variable names look like camels rising one after another, hence the name. The key to the camel case method is: except for the first word, the size of the first letters of other words, a mixture of uppercase and lowercase letters to form variable names and function names, and underscores can also be used to form logical breakpoints, which can enhance the readability of the code. .
What kind of naming convention to use depends on personal preference or company regulations. Note: When naming, you cannot use reserved words or names that are too long and difficult to remember. Avoid using two similar variable names.The following are some commonly used Hungarian nomenclature prefixes:
Type | Prefix | Type | Example |
Array | a | Array | aItems(item) |
String | s | String | sUserName |
Function | fn | Function | fnHandler(Handler) |
Object | o | Object | op |
Integer | i | Integer | iItemCount(item count) |
Floating point number | f | Float | fPrice(Price) |
Boolean | b | Boolean | bIsComplete(Complete) |
Regular Expression | re | RegExp | reEmailCheck(Email Detection) |
The above is the detailed content of JS naming convention: Hungarian nomenclature or camel case nomenclature. For more information, please follow other related articles on the PHP Chinese website!