---scopes in javascript

Source: Internet
Author: User

Scope:
Variables also have a range of function functions.
The browser's kernel has two main functions, one is the rendering page and the other is our JavaScript interpreter.

We mainly talk about how the JavaScript interpreter works in parsing.

At the time of parsing, pre-parsing occurs first, that is, it does not go directly to the line to execute the code we write, it will first find out what the declared variables, meet the function body when the function is placed in the top position, when the variable name and function name when the function will replace the variable declaration.
When parsing is a function body, the variables or functions in the function body are pre-processed, knowing what is declared.
If it is a variable, then the variable in the function body is called, and if not in the function body, the outer variable is found

Function Pre-parsing:
Will refer to the top of the function
Variable Pre-parsing:
Will know beforehand if there is a variable, but will not change the parse location

When pre-parsing, when the variable and function have the same name, the function overrides the variable, and the same name only has one


Global scope-accessible from anywhere
Variables defined outside the function have global scope
Variables that are not defined with VAR have global scope
Properties on all Window objects have global scope
Local scope-can only be accessed inside a function
Use Var to have local scope for variables defined inside a function


Global: can be called from anywhere
Local: Can be called only in the current scope (functional body)

1<script>2     varA = 10;//Global3     functionfn () {4Console.log (a);//undefined5         varA = 5;//Local6Console.log (a);//57     }8 fn ();9             Ten</script>

The concept of closures
function nesting function, the inner layer function can access the variables in the outer function

1<script>2     functionfn1 () {3         varA = 1;4         functionfn2 () {5a++;6 Console.log (a);
return A;7 }8 fn2 ();9 }Ten fn1 (); One</script>

There are two big uses for closures:

One is to read the variables inside of I outside the function body, and the other is to keep the variables in memory.

---scopes in 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.