JavaScript variables: Variable naming principles

Source: Internet
Author: User
Tags naming convention

The name of the variable is relatively not much technical content, today collation related to the principle of variable naming, mainly to tell you, although the name of no technical content, but for the personal code, or a team of the re-development and reading is very useful. Good writing practices can make your JavaScript code a step up, and it's better for the team to develop and read the code again.

Full Name principle
    • Variable names are case-sensitive, allow letters, numbers, dollar signs ( $ ), and underscores, but the first character is not allowed to be a number, not allowed to contain spaces and other punctuation
    • Variable name length should be as short as possible, and grasp the key points, as far as possible in the variable name to reflect the value of the type
    • Try to avoid the use of meaningless naming
    • Prohibit the use of JavaScript keyword, reserved word full name
    • The name method of variable names is commonly used in Hungarian nomenclature, hump nomenclature and Pascal's nomenclature.
Variable naming methods

The methods of naming variables include Hungarian nomenclature, hump nomenclature and Pascal's nomenclature. Here is a simple look at the use of these three nomenclature and their differences.

Hungarian Naming Act

Hungarian nomenclature Hungarian nomenclature is a kind of variable naming rule in computer program design, which can be subdivided into: System Hungarian nomenclature and Hungarian applied nomenclature method.

Hungarian nomenclature is a language-independent feature and is used for the first time in BCPL languages. Since BCPL is only a data type of machine word, the language itself does not help programmers to remember the types of variables. The Hungarian nomenclature solves this problem by clarifying the data type of each variable.

In Hungarian nomenclature, a variable name starts with one or more lowercase letters that help to memorize the type and purpose of the variable, followed by whatever name the programmer chooses. The first letter of this second part can be capitalized to distinguish the preceding type from the letter.

Hungarian nomenclature:

变量名=数据类型+对象描述
    • data type : Pointing is one of six data types in JavaScript,,,,, undefined null boolean number string andObject
    • Object Description : Refers to the full name of the object or part of the name, and must have a clear meaning, easy to remember and better understand

After ES6, the type of data is no longer six, its new symbol this new data type, there are related to the symbol of the introduction, you can read the article "in-depth analysis of es6:symbol."

Let's look at an example

var aPerson = []; // Array数组var oBtn = document.getElementById(‘btn‘); //Object对象var fnName = function () {}; // function函数var sName = "w3cplus"; // string字符串

As in the example above, the variable aPerson name oBtn , fnName or sName . Each variable name is preceded by a corresponding letter representing the data type, followed by multiple words with a meaningful single word name, and the letters of the words are capitalized (in fact, this method, called Hump writing, is described later).

The JavaScript variable name represents the data type in the corresponding Word line, as follows:

    • s: A string that represents strings
    • i: Represents an integer int (it is an integer type in number)
    • fl: Represents float float (it is a decimal type in number)
    • b: Represents a Boolean Boolean
    • a: Represents an array of arrays
    • o: Represents object
    • fn: function functions are not shown
    • re: Indicates regular regular expression

For more details about naming Hungary, click here to read.

Hump Naming method

When the variable name and function name are linked together by two or more words, and the unique identifier is made up of a "camel case", you can increase the readability of the variables and functions.

The term camel case (camel-case) comes from a common case-and-write format used in the Perl language, and the cover picture of Programming Perl (O ' Reilly, published by Larry Wall and others) is a camel. ”

The "Camel case" naming convention can be considered as a convention, and there is no absolute and mandatory to increase recognition and readability. Once you have chosen or set up a naming convention, you should maintain a consistent format when you write your program.

There are two common forms of hump naming:

    • Small Hump-type nomenclature (lower camel case): The first word begins with a lowercase letter, and the first letter of the second word is capitalized, for example: firstName lastName .
    • Large Hump-type nomenclature (upper camel case): the initials of each word are capitalized, for example:, FirstName , LastName CamelCase , also known as the Pascal Nomenclature.

There are more descriptions about camel-named methods, which can be read here.

Pascal name Law

Pascal Nomenclature (Pascal case, carrier nomenclature/Pascal nomenclature), a set of naming conventions (conventions) when a computer program is written.

When the variable name and function name are the unique identifiers that are linked together by two or more than two words, the readability of variables and functions is increased.

The words are not separated by a space or connection number ( - ), underscore ( _ ), the first single-prefix letter is capitalized, and the first letter of subsequent words is also capitalized, for example: FirstName , LastName . The initials of each word are named in uppercase letters, known as the Pascal Nomenclature, and are derived from the naming conventions of the Pascal language, also known as the "big hump-type nomenclature" (Upper Camel case), a subset of camel-cased.

"Pascal nomenclature" can be regarded as a naming convention, and there is no absolute and mandatory, in order to increase recognition and readability. Once you have chosen or set up a naming convention, you should keep the formatting consistent when you write your program.

There is a more detailed introduction to Pascal's nomenclature can be read here.

Although the above three methods are commonly used to name variables in JavaScript, they are also recommended by many specifications, but there are other ways. For example, when a variable name has two or more words, it can be used or connected between multiple words - _ . Like first-name or say first_name .

Summarize

This article briefly describes some of the principles that are related to naming variables in JavaScript. and introduces several common methods of variable naming, such as Hungarian nomenclature , Hump nomenclature and Pascal's nomenclature . Regardless of the method used to name a variable, the article begins by mentioning that the variable name is case-sensitive, allowing for letters, numbers, dollar signs ( $ ), and underscores, but the first character is not allowed to be a number, not allowed to contain spaces and other punctuation and Variable names prohibit the use of JavaScript keywords, reserved word full name two points, otherwise it is easy to bring a catastrophic error in the code.

If you need to reprint, please indicate the source: http://www.w3cplus.com/javascript/variable-naming-principles.html

JavaScript variables: Variable naming principles

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.