Ultra comprehensive variable naming rules in javascript

  • 2021-07-18 06:51:34
  • OfStack

Preface

Relatively speaking, the naming of variables does not have much technical content. Today, there are rules related to naming variables, mainly to tell you that although naming has no technical content, it is quite useful for personal coding, or re-development and reading by a team. Good writing specifications can take your JavaScript code to a higher level, and it is also more conducive to the team's re-development and reading of the code.

Principle of full name

Variable names are case sensitive and are allowed to contain letters, numbers, dollar signs ($), and underscores, but the first character is not allowed to be a number, and spaces and other punctuation marks are not allowed The variable name length should be as short as possible, and grasp the key points, and try to reflect the type of value in the variable name Try to avoid using meaningless names It is forbidden to use JavaScript keywords and keep the full name of words The common naming methods of variable names are Hungarian naming, hump naming and Pascal naming

Hungarian nomenclature

Hungarian nomenclature Hungarian nomenclature is a variable naming rule in computer programming. This nomenclature can be subdivided into systematic Hungarian nomenclature and Hungarian applied nomenclature.

Hungarian nomenclature is language independent and is used extensively in BCPL language for the first time. Because BCPL has only one data type, machine word, the language itself cannot help programmers remember variable types. Hungarian nomenclature solves this problem by specifying the data type of each variable.

In Hungarian nomenclature, a variable name begins with one or more lowercase letters, which help to remember the type and purpose of the variable, followed by any name chosen by the programmer. The first letter of this second half can be capitalized to distinguish the previous type indicating letters.

Grammar

Variable name = type + object description

Type refers to the type of variable Object description refers to the full name of the object or the first part of the name, which requires clear meaning and easy to remember and understand.

The type of variable is identified by prefixing the variable name with a symbol of the corresponding lowercase letter followed by a combination of one or more words that describe the purpose of the variable. If it is an jquery object, prefix the variable name with $.

Prompt

Although javascript variables have no type on the surface, javascript still assigns corresponding types to variables.

Hungarian nomenclature was invented by a Microsoft programmer and used by most c and c + + programs.

Type

javascript 变量命名类型 变量命名前缀
array 数组 a
boolean 布尔值 b
float 浮点数 l
function 函数 fn
int 整型 i
object 对象 o
regular 正则 r
string 字符串 s

Example


var aName = [1, 2, 3];
var oBtn = document.getElementById('btn');
function fnName(){};
var iCount = 0;
var sName = "zhuyujia";

Hump nomenclature

When the variable name and function name are only one identifier formed by linking two or more words in one, using "hump case" to express them can increase the readability of variables and functions.

"The word hump case (Camel-Case) comes from the mixed case format commonly used in Perl language, and the cover picture of the best-selling book" Programming Perl "written by Larry Wall et al. (published by O 'Reilly) is a camel."

The naming rule of "hump case" can be regarded as a convention, which is not absolute or mandatory, in order to increase recognition and readability. 1 Once the naming rules are selected or set, the format of 1 should be kept when programming.

Grammar

Variable names or function names are connected by one or more words at the beginning of one, in which the first word starts with lowercase letters, and the first letters of all subsequent words use uppercase letters. Such variable names look like Camel Peak 1 one after another, hence the name.

Example


var myName = "zhuyujia";
var formSubmit = document.getElementById("submit");
function timeCount(){}

Pascal nomenclature

Pascal nomenclature (Pascal Case, Pascal nomenclature/Pascal nomenclature), a set of naming rules (conventions) in computer programming.

When a variable name and a function name are one-only identifiers formed by linking two or more words at the beginning of one, it is used to increase the readability of variables and functions.

Words are not disconnected by spaces or linked by connecting numbers (-) and underscores (_), and the first single prefix letter is uppercase; The initials of subsequent words also use capital letters, such as FirstName and LastName. The first letter of every word adopts the naming format of capital letters, which is called "Pascal Nomenclature", which originates from the naming convention of Pascal language, and some people call it "Big Hump Nomenclature" (Upper Camel Case), which is a subset of hump case.

"Pascal nomenclature" can be regarded as a naming convention, which is not absolute or mandatory, in order to increase recognition and readability. 1 Once the naming rules are selected or set, the format should be kept in the programming.

Grammar

It is similar to hump nomenclature, except that the first letter of the word needs to be capitalized.

Example


var MyName = "zhuyujia";
var FormSubmit = document.getElementById("submit");
function TimeCount(){}

Summarize

The above is the whole content of this article. I hope the content of this article can bring 1 certain help to everyone's study or work. If you have any questions, you can leave a message for communication.


Related articles: