[JavaScript tutorial] JavaScript Scope

Source: Internet
Author: User
Set of accessible variables in the scope.

JavaScript Scope

Set of accessible variables in the scope.

JavaScript Scope

In JavaScript, objects and functions are also variables.

In JavaScript, the scope is a set of accessible variables, objects, and functions.

JavaScript function scope: the scope is modified within the function.

JavaScript local scope

Variables are declared in the function, and the variables are local scopes.

Local variables: they can only be accessed within the function.

Instance

// The carName variable cannot be called here

Function myFunction (){
Var carName = "Volvo ";

// The carName variable can be called in the function.

}


Try again»

Because local variables only apply to functions, different functions can use variables with the same name.

The local variable is created when the function starts to be executed. After the function is executed, the local variable is automatically destroyed.

JavaScript global variables

A variable is defined outside a function, that is, a global variable.

Global variables have a global scope: All scripts and functions on the web page can be used.

Instance

Var carName = "Volvo"; // The carName Variable function myFunction () can be called here {// The carName variable can be called in the function}

If the variable is not declared in the function (the var keyword is not used), the variable is a global variable.

In the following example, carName is in the function but is a global variable.

Instance

// The carName Variable function myFunction () {carName = "Volvo" can be called here; // The carName variable can be called here}

JavaScript variable Lifecycle

The JavaScript variable lifecycle is initialized when it is declared.

Local variables are destroyed after the function is executed.

Global variables are destroyed after the page is closed.

Function Parameters

Function parameters only work in the function. They are local variables.

Global variables in HTML

In HTML, global variables are window objects: All data variables belong to window objects.

Instance

// Here you can use window. carNamefunction myFunction () {carName = "Volvo ";}

Do you know?

Your global variables or functions can overwrite the variables or functions of the window object.

Local variables, including window objects that can overwrite global variables and functions.

The above is the content in the JavaScript scope of the [JavaScript tutorial]. For more information, see the PHP Chinese website (www.php1.cn )!

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.