JavaScript Knowledge Schema Learning Path (i)-Variable chapter

Source: Internet
Author: User
Tags object object

"Summary"This article is intended for JavaScript beginners, and generally divides the basic part of JavaScript into nine chunks, namely variables, operators, arrays, process control structures, string functions, function Foundations, DOM manipulation basics, Document Object Model DOM, and regular expressions.

Keywords variables, operators, arrays, process control structures, functions, DOM, regular expressions.

The main content of this article is around the basis of JavaScript (hereafter referred to as JS) summarized and summarized, on the one hand can give entry-level JS enthusiasts to provide a learning path, on the other hand, you can also give a slightly basic JS learners to comb the knowledge.

As a Web page design three-layer separation structure of the third part, JS role and status is undoubtedly, however, want to systematically grasp the basis of JS, for the follow-up of high-level learning to pave the ground, but also must have a clear understanding of the basic content of JS, so, in this will be the basis of JS is divided into nine large modules to be

First, JS variable

For variables, we mainly need to grasp from four aspects, including naming, declaration, type and scope, these four aspects in other programming languages also exist, the following we are specific to summarize:

1. Naming

In terms of the naming of variables, we need to understand named methods and named rules. I

(1) Method of naming

Named method in the actual project, we usually have two major categories, one is the Hungarian nomenclature, the name of the method is: variable name = Type + Object Description, for example, when we want to name a form variable, we first know that the variable is the type of the form (form), and We want to know the function or meaning of this form, for example, this form is a switchboard, so at this point, we should name this form variable is frmswitchboard, so that, other people in reading your code, it is easy to understand, when you see the frm, I knew you. This variable is a form variable.

In addition to the Hungarian nomenclature, the other category is the Hump nomenclature, which is often used in most of the current project naming methods, and the hump naming method is also divided into uppercase and lowercase. If all are lowercase names, then each of the two words is connected using an underscore "_", for example: Frm_switch_board; If the capitalization of the name exists at the same time, it is divided into large hump and small hump. The name of the big Hump is the first letter of each word is capital letters, for example: Frmswitchboard; and the name of the small hump is the first letter to lowercase, followed by the first letter of the word should be capitalized, for example: Frmswitchboard.

The above is the name of the variable method, at present, the main use of the domestic naming method is the small hump name method. So, while we understand the naming method, we must also master the type of problem, in JS, the types are mainly the following:

<1>int Type-I.

<2>float type-FL

<3>boolean Type-B

<4>string Type-S

<5>array Array-A

<6>object Object-O

<7>function function-fn

<8>regular Expression Regular-re

Now that we know the type, let's look at the rule of variable naming.

(2) Rules for naming

The reason we want to know the naming conventions of variables is to make our named variables legal, which means we can use them normally.

In JS, the rules for naming variables are generally subject to the following three articles:

<1> the first character of a variable name can only be an English letter or an underscore;

<2> variable names can consist of English characters, numbers, and underscores, and cannot contain other special characters, such as @,! and other characters;

<3> variable names cannot be keywords or reserved words in JS.

2. Statement

After we have fully understood and mastered the naming of variables, let's look at the declaration of variables. For the declaration of variables, we also need to grasp the following three principles:

(1) The variables in JS are usually explicitly declared, for example: VAR variable name;

(2) When declaring variables, we should avoid a few problems. One is that the declared variable has no data type, and the other is that the variable is declared repeatedly; its three implicitly declares the variable; its four is not declared but directly assigned.

One of the simplest ways to solve this kind of problem is to write the variable-declarator var first, then write the variable name, and then assign the initial value directly to the variable, for example: "var i = 0;" ;

(3) We also need to develop a good habit of declaring variables, a simple summary of 12 words: first declaration, after reading and writing, first assignment, after operation.

3. Type

For a variable, it can be divided into two categories, one is the value type, and the other is the reference type.

(1) Value type

For a variable of value type, it has the following characteristics:

The storage space occupied by <1> is fixed;

<2> at the time of saving and copying, the operation is the value itself;

<3> can use the type detection function typeof () in JS to detect its specific type;

(2) Reference type

A variable of reference type, it also has several characteristics:

The storage space occupied by the <1> variable is not fixed;

<2> at the time of saving and copying, the operation is a pointer to the variable;

<3> can use the instanceof () function to detect its data type

At the same time, one thing we need to know is that the object constructed with the new () method is a variable of the reference type.

4. Scope

After we understand the names, declarations, and types of variables, let's take a closer look at the other key question of the variables we use in the actual project, which is the scope of the variables. Here, we first divide the variables into three levels based on the scope of the variables, such as global variables, local variables, and parametric variables.

For the above level three variables, we summarize from the writing to the call, then to the priority and finally to the feature, and so on three levels.

(1) Notation and invocation

For global variables, it requires us to declare this variable when it is declared outside the body of the function, and when it is called, it can be done anywhere in the project, and for local variables, it requires that we declare within the body of the function, and when called, it can only be called within the body of the function; For a parameter variable, it is also required to be declared at the time of the function declaration, while the invocation must also appear inside the body of the function.

(2) Priority level

In terms of priority, there are three reference rules:

<1> local variables with the same name have higher precedence than global variables;

<2> the precedence of parameter variables of the same name is higher than global variables;

<3> local variables with the same name have higher precedence than parameter variables.

(3) Features

For the scope of variables, there are some characteristics:

<1> Global variables can be considered as properties of global objects, while local variables can be considered as properties of the calling object;

The <2> inner function can access the local variables of the outer function, but the outer function cannot access the local variables of the inner layer function;

<3> in the life cycle of a variable, the global variable will persist unless it is deleted, and the local variable survives the declaration until the function finishes or is deleted.

The above content is we in the study of JavaScript, the variables need to grasp the knowledge points, hope to bring you a reference.

Excerpt from: Here

JavaScript Knowledge Schema Learning Path (i)-Variable chapter

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.