What you need to know to learn JavaScript.

Source: Internet
Author: User
Tags variable scope

First: Variable declaration

The first thing to do when using a variable with JavaScript is to declare the variable, and the keyword for the variable declaration is var.

Example:

var i;

var sum;

You can also declare multiple variables:

var i,sum;

You can also assign a value at declaration time.

var i=0;

If you do not specify an initial value for a variable declared by VAR, the variable is always undefined before it is given its initial value.

JavaScript is a dynamic language that does not do data type checking until it is run, so you do not need to specify a data type for the variable, it will record the data type at the first assignment.

C and Java are not the same, they are static languages, need to be defined first, the data type is OK.

Example:

var i=0;

I= "Len";

It is completely legal to assign a value to it before assigning it a value of another data type.

Second: Statements of repeated declarations and omissions


It is legal to declare variables repeatedly using the VAR statement, which is no different from an assignment language.

This omission statement is not the same, the omission of the statement to see where you are missing, this is going to pay attention to, because of global variables and local variables cause.

A global variable is a variable that is valid anywhere in the JavaScript code (provided that the variable is not declared).

This omission, however, becomes a global variable, but it is somewhat different from the variables you declare in the global scope, although they

It doesn't seem to make much of a difference, but you know that a variable that's not declared actually becomes a custom property, and a custom window that's low

Properties, such as:

V=2;===>window. v=2;

Because in the global scope, window can ignore not write, so often forget the window, so it can be deleted, in

When you use the delete operator.

Example:

var truevar=1;

Delete Truevar; Declares that it cannot be deleted.

fakevar=2;

Delete Fakevar; The variable was deleted

Third: Variable scope

The scope of a variable is defined in the program source code in the area of the variable, global variables have a global scope, in the JS code anywhere

is defined. However, variables declared within a function are defined within the function, they are local variables, the scope is local, local is the local

Use, go out this scope will be invalid, function parameters are local variables, they are only defined in the function body.

In a function body, a local variable takes precedence over a global variable of the same name, if the function body declares a variable in a local variable or function parameter and a full

If the local variable is the same name, the global variable will be overwritten.

Example:

var scope= "global";

function part () {

var scope= "part";

alert (scope);

}

Part (); The popup is a part

alert (scope); The popup is a global

Example:

var scope= "global";

function part () {

Scope= "part";

alert (scope); Eject part

}

Part ();

alert (scope); The popup is a part

The difference above is that in a function there is no repetition of a variable with the same name as a global variable. The effect from the code is obvious, and there is no declaration in the function body

is a global variable, so the value is changed once it is re-assigned.

Functions can be nested, because each function has its own scope, so there are several local scope nesting cases.

Example:

var scope= "1";

function part () {

var scope= "2";

function Part1 () {

var scope= "3";

alert (scope); 3

}

alert (scope); 2

}

alert (scope); 1

Do not parse, think about yourself.

IV: Function scope and declaration in advance

In some languages curly braces are the scope of a piece of code, and the declaration of a variable outside the scope is

Invisible, we call this a block-level scope. JS has no block-level scope, and it has a function scope, which changes

Quantities are defined by the body of the function in which they are declared and by any function within which the body of the function is nested.

function Test () {

var i=0;

if (i==0) {

Console.log (i); 0

}

}

The function scope of JS refers to all the variables declared within the function are always visible in the function body, how does this understand?

is if you use the variable in the body of the function and then declare the variable, it will understand that you have declared

This local variable, so you're using this local variable, no matter where you declare it in the function body, in the function

Where the body uses it, it will resolve to you declare it as a local variable, you can use it in this part, will not change

A variable with the same name as the global.

Example:

var i=0;

function part () {

alert (i);

var I=1;

}

That is the case, he will not pop up 0, but will not pop up 1, because 1 of the assignment is after using this function, so

It will pop up undefined.

Does this mean that a variable is visible and usable before it is declared?

And this is called "declaration in advance." Is that all functions declared within a function are advanced to the top of the function.

V: Scope Chain


It's easy to understand. When code executes in an environment, the first object of the scope is always the current execution code

The variable object in the environment.

If the execution environment is a function, then its active object is the first object of the scope chain, and the second object is the containing environment,

The next is the containment environment that contains the environment ..... Until you find the one you want to find.

The parsing of identifiers during a function run is the process of searching along a chain of scopes, starting with the first object,

Level backwards until the identifier of the same name is found, and no further traversal is found and an error is not found.

These need to understand, not to write JS code after the problem will be very laborious.

The following is a simple description of the above knowledge:

Scope:
Domain: space, range, area ....

Function: Can read and write operations in the spatial range

JS Pre-parse//This is a declaration of advance understanding


1) "Find something": var function parameter

A= Not defined

All variables, before formally running the code, assign a value in advance: undefined

Fn1=function fn1 () {alert (2);}

So the function, before it's officially run, is the entire function block.

2) Line-wise interpretation of code


Expressions: =, + 、-、 *,/,%, etc. are expressions, expressions are actions, can change values


1) If the name is the same, only one store to the warehouse, pre-analytic first in the function block, and then priority, from top to bottom, the greater the priority;

2) then read the code in line
Execute expression takes precedence when you see an expression


Domain is the first and next, is single-threaded, processing one after another to the next, like the introduction of libraries;

function is also a domain, just a local domain;

The pre-parsing steps are performed;

If there is a variable inside, it takes precedence over the variable, and the local variable takes precedence

The child scope is scoped to the parent scope, which is the scope chain

Parsing process

1) Pre-parsing

2) Read the Code by line:

An expression

Function call:

1) Pre-parsing

2) Line-wise interpretation of code


The argument is that in the function Var claims a variable and assigns it to him.

To get the values in the function:

Global variables

function arguments in a function

What you need to know to learn JavaScript.

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.