Scopes in JavaScript

Source: Internet
Author: User

The concept of scope in JS:

Represents the area in which a variable or function works, referring to the context in which they are executed, i.e. the context execution environment. There are only two scopes for javascript: global scope and local scope, and local scopes are distinguished by functions.

First look at several topics:

1.

The code is as follows:
if (true) {  var aa= "BB"//for (var i = 0; i < 100 ; i++) {  / ///

2.

The code is as follows:
var bb = ' 11111 'function  aa () {     alert (BB); //     var bb = ' test ';     Alert (BB); //     var cc = "Test1";     alert (age); //

3.

The code is as follows:
var test = ' 1111111 'functionfunction  bb () {  var test = '   22222222 '; AA (); } BB (); //

4.

The code is as follows:
Alert (typeof//alert (typeof//undefinedfunction //  alert (' I am 111111111 'varfunction//} alert (  typeof BB); //

5.

The code is as follows:
function   AA () {  var bb = "Test"= "testing"; Alert (BB); } AA (); Alert (cc); //  Alert (BB); //

The above 5 topics all summarize the scope of JS in the problem


Can sum up so many points

One, no block-level scope

As you can see from the first question, when executed in {}, the variables are not destroyed or stored in memory, so we can access them.

The functions in JavaScript run in the scope they are defined in, not in the scope in which they are executed.

The concept of the scope chain of a function is mentioned here, in ECMA262.

The scope of any execution context time is implemented by the scope chain (scope chain).
When a function is defined, the scope chain that it defines is linked to the [[scope]] property of the function object.
When a function object is called, an active object (that is, an object) is created, and then, for each function's formal parameter, it is named as the active object's named property, then the active object is the front-end of the scope chain (scope chain) at this time, and this function object's [[ Scope]] added to scope chain.
So the title 3 result is alert (1111111);

JS will process the function definition and the var keyword in advance.

Start alert (BB) as topic 4; Undefine, alert (age)//syntax error, what is the difference between the two, the reason is that there is a var bb = "Test", at the time of initialization to deal with the keyword of Var, but this is not the beginning of the assignment

Modify the code so that you can see

The code is as follows:
var dd = ' 11111 'function  aa () {     alert (BB); //     var bb = ' test ';     Alert (BB); //     var cc = "Test1";     alert (age); //  } aa (); Alert (dd); //  alert (cc); //

Here alert (BB) does not report a syntax error, alert (age) reported a syntax error.

However, please note that:

The code is as follows:
<script> alert (typeof//function  AA () {alert (' Yupeng '

This means that JS precompilation is a unit of segments. Topic 4 likewise


Iv. Function-level scopes

The defined variables inside the function are destroyed after the function is executed and do not occupy the memory area.

So the topic 2 the last alert (cc); Grammar error, topic 5 finally to alert (BB) the same

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.