JavaScript Knowledge points Summary (naming conventions, scope of variables)

Source: Internet
Author: User

Naming conventions

Some people say that the tolerance of JavaScript is one of the worst aspects of the language. For example, if you want to add 2 numbers together, JavaScript will parse one of the numbers into a string, then you will get a strange string instead of 2 digits.

When it comes to JavaScript data types, the environment is everything. This article covers 3 types of data: String type, Boolean type, and numeric type.

JavaScript variables are similar to variables in other languages. They are used to hold values, and the saved values can be accessed in different places of the code. The identifier for each variable is the same within the scope of the scopes it uses. JavaScript variables are case-sensitive, such as StringBuilder and StringBuilder, which are 2 different variables. In addition, the name of the variable cannot be a keyword, and the following table helps you quickly query which keywords are.

Break Else New Var
Case Finally Return void
Catch For Switch While
Continue function This With
Default If Throw
Delete Inch Try
Do instanceof typeof

The above is the reserved word of ecma-script, of course, there are some JS special words, they are reserved words in the browser, these can not be used as variables, such as the following table:

Alert Eval Location Open
Array Focus Math Outerheight
Blur function Name Parent
Boolean History Navigator Parsefloat
Date Image Number Regexp
Document IsNaN Object Status
Escape Length OnLoad String

Here are some naming conventions, of course, not mandatory:

For collections, the name should be in the plural:

var customernames=new Array ();

If you want to declare an object, the first letter of the object name is capitalized.

var firstname=new String (' Peter ');

Both the function and the variable start with a lowercase letter:

Function Validatename (Firstname,lastname)

Prototype Naming method:

Many of the new nomenclature is not intended to make the language readable, but rather to bring the language closer to other languages, which is even more beneficial for backend developers learning the front end.

The following dash begins to represent a private variable:

var _object= new Object ();

Prototype also uses $ to express the shortcut method, the most notable example being jquery. The name of the function should use a verb + noun, and the variable name should preferably use a noun, for example:

var currentmonth;function returncurrentmonth ()

If it is a loop, try to use simple representations, such as I,j,k.

Scope

Let's compare the following 2 lines of code:

Num_value=3;var num_value=3;

As I mentioned before, JavaScript does not enforce the use of the VAR keyword when declaring a variable, but the difference between the 2 is the difference in scope. There are the following benefits:

To prevent conflicts between local variables and global variables of the same name, such as a page reference 2 JS files, and the 2 JS files, there are 2 global variables of the same name, which will result in two semantics, the specific code you can see after you try it, is an HTML file reference 2 JS file , and then the 2 JS files have 2 variables with the same name without Var, give 2 different values, you can see how the output, here is no longer demonstrated.

  

  

JavaScript Knowledge points Summary (naming conventions, scope of variables)

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.