JavaScript (1)--variables, function declarations, and scopes

Source: Internet
Author: User

This is my first blog post, I am not, writing is not good, so may write a bit messy. There is nothing wrong with the place also hope forgive. But one small step a day will take that big step someday. The following is my understanding of variables, function declarations and function expressions, scopes.

Variable
In effect, a variable is a placeholder that defines a value that can be used to hold values. Use the var operator when defining variables.

The saved type of the variable can be changed, but is not recommended.

The variable defined by the var operator becomes a local variable in the scope of the variable, and after the function exits, the variable is destroyed.

Omitting the var operator allows you to define a global variable, but in strict mode it causes a Referenceerror error to be thrown.

You can define multiple variables with a single statement, as long as you separate each variable with a comma.

such as: Var massage= "Hi",
Found=false;

In strict mode, a variable named eval or arguments cannot be defined, resulting in a syntax error.

"Function declaration and function expression"

Functions: are defined once, can be executed and called multiple times, JS function is also an object, JS function can be like other objects to manipulate and pass.

There are two common ways to create a function: A function declaration followed by a function expression.

function declaration: A complete statement begins with a function, without adding anything. such as: function name (parameter) {function Body}

function Expressions: There are four forms of expression. Assign an anonymous function expression to a variable. such as: var variable =function (parameter) {function Body};

Enclose an anonymous function directly in parentheses and then call it directly. such as: (function () {Functional Body}) ();

Let the function object act as a return value. such as: return function () {function Body};

The function expression is not anonymous () and assigned to a variable. such as: var variable =function function name (parameter) {function Body};

Difference: The function declaration is pre-placed. The parser first reads the function declaration and makes it accessible before executing any code, and as for the function expression, it must wait until the parser executes to its line of code before it is actually interpreted.

"Scope"

Scope is a range of functions and variables that can be accessed, there are two important concepts are function scope and block-level scope, but in JS, there is no block-level scope.

Block-level scope: In other Class C languages, blocks of code that are enclosed by {}. such as: if{}, for () {}. In other Class C languages, the variables in the IF statement are destroyed after the IF statement is executed, whereas in JS, the variable declaration of the IF statement adds the variable to the current execution environment.

In JS, there are global scopes, function scopes and eval scopes. The global scope is generally declared at the outermost layer, and the function scope has its own independent scope, and outside of the function is a variable that cannot be accessed inside the function.

When code executes in an environment, a scope chain of variable objects is created. A scope chain is an orderly access to all variables and functions that guarantee access to the execution environment. The front end of the scope chain, which is always the variable object for the environment in which the code is currently executing. The next variable object in the scope chain comes from the containing (external) environment, and the next variable object comes from the next containment environment. Continues to the global execution environment, the variable object of the global execution environment is always the last object in the scope chain.

In the execution environment, the internal environment can access all the external environment through the scope chain, the external environment cannot access any variables and functions in the internal environment, the relationship between the environment is linear and sequential.

There are both global and local types of execution environments, but there are other ways to extend the scope chain. A variable object can be temporarily added to the front of the scope chain, and the variable object is removed after the code executes. As follows:

Catch block for Try-catch statement: Creates a new variable object that contains the declaration of the wrong object being thrown.

With statement: The specified object is added to the scope chain.

JavaScript (1)--variables, function declarations, and scopes

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.