The scope of JavaScript is related to collation.

Source: Internet
Author: User

JavaScript scope related collation.

Global scope (global scope is an object that can be accessed throughout the code)

The first global scope is the outermost defined variable

var toDay = "Tuesday"; function func () {    var yesterday = "Monday";     function Func2 () {        console.log (yesterday);    }    Func2 ()}alert (today); {#提示Tuesday #}alert (yesterday); {#报错 #}func (); {#打印Monday #}func2 (); {#报错 #}

1, the outermost declared variable, can be called.

2, the outside function declares the variable, inside the function can be called.

3, the variables declared inside the function cannot be called outside.

4, the function inside the function cannot be called outside.

The second is the variable that does not write Var

function func () {    = "Tuesday";     var yesterday = "Monday";    Alert (today)}func (); {#执行函数, hint tuesday#}alert (today); {#提示Tuesday #}alert (yesterday); {#报错 #}

No definition, direct assignment, automatically declared as a global variable.

Also, the properties of all window objects have global scope.

In general, the built-in properties of the Window object have global scope, such as Window.name, Window.location, Window.top, and so on.

Local variables (inside a function, when local variables have the same name as global variables, local variables take precedence over global variables, but do not overwrite global variables in memory areas:)

Not to be continued ....

Summarize:

JavaScript manages scopes through functions.

Local variables: variables declared inside a function are local variables that can only be used inside the function, outside of which the function is not available.

global variable: A variable declared outside of any function, or a variable that is simply used directly inside a function without being declared.

Global objects: each JavaScript environment has a global object that can be accessed when you use this in the appropriate place. Each global variable you create is a property or array element of this global object. In the browser, for convenience, the global object has an attached property called window, which is the window (typically) that points to the global object itself.

The scope of JavaScript is related to collation.

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.