JavaScript JavaScript Tutorial

Source: Internet
Author: User

A JavaScript variable can be a local variable or a global variable.

A private variable can be used to close the packet.

Global variables

Functions can access variables defined inside the function, such as:

instance function myFunction () {
var a = 4;
Return a * A;
}
Try it?

Functions can also access variables defined outside the function, such as:

instance var a = 4;
function MyFunction () {
Return a * A;
}
Try it?

In a later instance, A is a global variable.

The global variable belongs to the Window object in the Web page.

Global variables can be applied to all scripts on the page.

In the first instance, a is a local variable.

A local variable can only be used to define its internal function. It is not available for other functions or script code.

Global and local variables, even if they have the same name, are two different variables. Modifying one of them does not affect the value of the other.

When a variable is declared without using the var keyword, it is a global variable, even if it is defined within a function.

Variable life cycle

Global variables are globally scoped, that is, throughout the JavaScript program, global variables are everywhere.

Variables declared inside the function only work inside the function. These variables are local variables, the scope is local, and the parameters of the function are local and only work inside the function.

Counter dilemma

Imagine if you want to count some values, and that counter is available in all functions.

You can use global variables, and the function sets the counter to increment:

Example var counter = 0;

function Add () {
return counter + = 1;
}

Add ();
Add ();
Add ();

The counter is now 3
Try it?

Counter values change when you execute the Add () function.

But the problem is that any script on the page can change the counter, even if the Add () function is not called.

If I declare a counter inside a function, the value of the counter cannot be modified if no function is called:

instance function add () {
var counter = 0;
Counter + = 1;
}

Add ();
Add ();
Add ();

The intention is to output 3, but backfired, the output is 1!
Try it?

The above code will not output correctly, and each time I call the Add () function, the counter will be set to 1.

The JavaScript inline function solves the problem.

JavaScript inline functions

All functions have access to global variables.

In fact, in JavaScript, all functions have access to the scope of their previous level.

JavaScript supports nested functions. A nested function can access the function variables of the previous layer.

In this instance, the inline function Plus () can access the counter variable of the parent function:

instance function add () {
var counter = 0;
Function Plus () {counter + = 1;}
Plus ();
return counter;
}
Try it?Related Tutorialsvb.net Tutorial SQL Statement Daquan ASP. NET Tutorial dreamweaver tutorial JavaScript Tutorial Flash tutorial VB Tutorial computer Two-level VFP Access Tutorial HTML tutorial C # Tutorial Database Tutorial I want to self-study web page design training software development I want to learn the net I want to learn network SQL Server 2008

If we can access the Plus () function externally, this will solve the counter's dilemma.

We also need to make sure that counter = 0 executes only once.

We need to close the bag.

JavaScript closures

Do you remember the function self-invocation? What does the function do?

instance var add = (function () {
var counter = 0;
return function () {return counter + = 1;}
})();

Add ();
Add ();
Add ();

Counter is 3
Try it?Instance parsing

The variable add specifies the return character value of the function self-invocation.

The self-invocation function executes only once. Set the counter to 0. and returns a function expression.

The add variable can be used as a function. The great part is that it can access the counter of the scope of the function.

This is called a JavaScript closure. it makes it possible for a function to have a private variable.

Counters are protected by the scope of anonymous functions and can only be modified by the Add method.

JavaScript JavaScript Tutorial

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.