Home >Web Front-end >JS Tutorial >Summary of examples of the most powerful JavaScript naming methods

Summary of examples of the most powerful JavaScript naming methods

伊谢尔伦
伊谢尔伦Original
2017-07-18 13:26:321595browse

javascript has three classic variable naming methods: Hungarian nomenclature, camel case nomenclature and Pascal nomenclature.

Rules:

  1. Variable names are case-sensitive and are allowed to contain letters, numbers, dollar signs ($), and underscores, but the first character is not allowed It is a number, and spaces and other punctuation marks are not allowed

  2. The variable naming length should be as short as possible, and the key points should be grasped, and the type of value should be reflected in the variable name as much as possible

  3. Try to avoid using meaningless names

  4. It is forbidden to use JavaScript keywords and reserved full names

  5. Common variable name naming methods include Hungarian nomenclature, camel case nomenclature and Pascal nomenclature

##Hungarian nomenclature

Hungarian nomenclature Hungarian nomenclature is a variable naming rule in computer programming. This nomenclature can be subdivided into: system Hungarian nomenclature and Hungarian application nomenclature.

Hungarian nomenclature has language-independent characteristics and is widely used in BCPL language for the first time. Since BCPL has only one data type, machine words, the language itself cannot help programmers remember the types of variables. Hungarian nomenclature solves this problem by clarifying the data type of each variable.

In Hungarian nomenclature, a variable name begins with one or more lowercase letters that help remember the variable's type and purpose, followed by whatever name the programmer chooses. The first letter of this second half may be capitalized to distinguish it from the preceding type designator letter.

Syntax

Variable name = type + object description


  1. The type refers to the type of the variable

  2. Object description refers to the full name or part of the name of the object. It must have a clear meaning and the name must be easy to remember and understand.

Indicate the type of the variable by adding the corresponding lowercase letter symbol as a prefix in front of the variable name. The prefix is ​​followed by one or more word combinations, and the words describe the purpose of the variable. If it is a jquery object, use $ as the prefix of the variable name.

Tips

Although javascript variables do not have types on the surface, javascript will still assign corresponding types to variables internally.

The Hungarian nomenclature was invented by a Microsoft programmer. Most c and c++ programs use this nomenclature.

Example

##

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

Camel case
When variable names and function names are unique identifiers formed by linking two or more words together, use "camel case" to express them, which can increase the readability of variables and functions. sex.

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

The "camel case" naming rule can be regarded as a convention, not absolute or mandatory, in order to increase recognition and readability. Once you choose or set up a naming convention, you should maintain a consistent format when writing your program.

Grammar

The variable name or function name is composed of one or more words connected together, where the first word starts with a lowercase letter, and all subsequent words are The first letters are all capital letters, so the variable name looks like a camel's hump, 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) when writing computer programs.

When variable names and function names are unique identifiers composed of two or more words linked together, it is used to increase the readability of variables and functions.

Words are not separated by spaces or linked by hyphens (-) or underscores (_). The first single prefix letter is a capital letter; the first letters of subsequent words are also capital letters, for example: FirstName ,LastName. The first letter of each word adopts the naming format of capital letters, which is called "Pascal nomenclature". It is derived from the naming convention of Pascal language. Some people also call it "Upper Camel Case" (Upper Camel Case). A subset of camel case.

"Pascal nomenclature" can be regarded as a naming convention, which is not absolute or mandatory, in order to increase recognition and readability. Once a naming convention is chosen or set, the format should be consistent when writing programs.

Syntax

is similar to camel case, except that the first letter of the first word needs to be capitalized.

Example

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

The above is the detailed content of Summary of examples of the most powerful JavaScript naming methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn