Writing maintainable JavaScript--javascript Coding Specification (ii)

Source: Internet
Author: User
Tags naming convention

Yesterday is my idol birthday, now the whole people are OK excited ah O (∩_∩) o~ gossip Less, let me first send an essay souvenir ^_^

Text Split Line//////////////////////////////////////

naming

As long as the code is written, variables and functions are involved, so variable and function naming is critical to enhancing code readability. The core of the JavaScript language is ECMAScript, which is followed by the camel case naming method. Camel case is used here by the author to refer specifically to the "Small camel Size" (that is, the first letter lowercase) nomenclature, Pascal case is referred to as the "large camel-cased" (that is, the first letter capitalized) naming method.

Small Hump-type capitalization is started by lowercase letters, followed by each word capitalized, such as: Var thisismyname;

Variables and functions

Variable names should always follow camel-case naming, and naming prefixes should be nouns.

A function name prefix should be a verb, so that variables and functions can be separated.

Good notation var count = 10;var MyName = "Nicholas"; var found = true;//bad notation: variable looks like var GetCount = 10;var Isfound = true;//good notation f Unction GetName () {      return myName;} Bad notation: function looks like variable function thename () {      return myName;}

In general, the name length should be as short as possible and catch the point. Try to reflect the worthwhile data type in the variable name. For example, naming count,length and size indicates that the data type is a number, while naming Name,title and message indicates that the data type is a string. But variables named with a single character, such as I, J, and K, are usually used in loops. Using these names to represent data types makes it easy for your code to be read by others and yourself.

To avoid the use of meaningless naming. Names such as Foo,bar and TMP should also be avoided, but there are a lot of them in the developer's toolbox that can be used with them, but don't let the names carry other additional meanings. For other developers, it is not possible to understand the usefulness of these variables without looking at the context.

For function and method naming, the first word should be a verb. Here are some common conventions for using verbs.

Verb Meaning
Can function returns a Boolean value
Has function returns a Boolean value
Is function returns a Boolean value
Get function returns a non-Boolean value
Set function to hold a value

Although these function naming rules are not included in the prevailing programming style, in many popular libraries, JavaScript developers will find many of these "pseudo-standards".

jquery obviously does not follow this function naming convention, partly because of the way methods are used in jquery, and many methods are used as getter and setter. For example, $ ("body"). attr ("class") can take the value of the class attribute. However, we recommend using verbs as prefixes for function names.

Constant

Before ECMAScript 6, there was no real concept of constants in JavaScript. However, this does not prevent developers from using variables as constants. In order to distinguish common variables and constants, a common naming convention arises. This convention is derived from the C language, which uses uppercase letters and underscores to name and underline words, such as:

var max_count = 10;

var URL = "http://www.nczonline.net/";

It is important to note that this is only a variable with a different naming convention applied.

constructor function

In JavaScript, a constructor is simply a function prefixed with the new operator, which is used to create an object.

The name of the constructor follows the large hump naming method. Doing so distinguishes the constructor from the variable and the normal function. Constructors are often named as nouns, because they are used to create instances of a type.

<!--

Fiber Sharp
Source: http://www.cnblogs.com/beginner2014
This article is copyrighted by the author and the blog Park, Welcome to reprint, but without the consent of the author must retain this paragraph, and in the article page obvious location to the original link. Thank you for your cooperation.

-

Writing maintainable JavaScript--javascript Coding Specification (ii)

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.