Explore the basic knowledge of JavaScript scopes _

Source: Internet
Author: User
This article mainly introduces the JavaScript scope. This article uses a concise language and an intuitive test result image to explain the JavaScript scope. If you need it, refer to the first article of the golden code:

Js does not have block-level scope (you can implement it by yourself using closures or other methods). It only has function-level scope. It can be found in variable functions outside the function and cannot be found outside the function.

First try:

Why ??

Var a = 10; function aaa () {// step-4alert (a); // step-5-> execute alert, at this point, you can only find the = 10 outside, so the bullet box 10} function bbb () {// step-2var a = 20; aaa (); // step-3} // defines the function is useless, the call is real, so here is the step-1bbb (); // step-1

As a matter of fact, everyone understands the principle. It should be easy and error-free.

Second try:

Why? When a is assigned B, B is not defined, so a is undefined and B is 10.

Article 2 of the golden code:

Variable Search is based on the proximity principle. Find the variable defined by var and find the outer layer when the proximity is not found.

Look:

Why ,? There are two reasons: Pre-resolution and nearby search.

Var a = 10; function aaa () {alert (a); // undefined, which will be searched in the function when searching for a. Due to the role of pre-resolution, at this time, a is undefined, so we will never find 10 out of the var a = 20;/* pre-Parse var aalert (a); var a = 20; */} aaa ();

Attention:

The second one is verified. Although the proximity principle is used, the variables declared by var are found nearby. This is because the variables declared without var are global, here, the value of a is modified. So the above is because the var a is not found in the function, so I went to the outside and found it as soon as I found it, so a will generate 10 alert; but it is true that after a = 20, a is indeed 20, but alert has not been executed yet ~~

Look ~

The following example further verifies the function scope of js:

This is because in alert (a), a in the bbb function is indeed 20, but it is partial for the alert (a) sentence at this time, alert () a In the bbb function is not found at all, so it cannot find a in the aaa function, so it almost finds it outside, and 10 as soon as it finds it.

Article 3 of the golden code:

When a parameter has a duplicate name with a local variable, the priority is the same.

Example:

In addition, when passing parameters, the basic type is passed as a value, and the reference type is passed as a reference. (This is not the case after the value is assigned again)

var a = 5;var b = a;b +=3;alert(a);//5var a = [1,2,3];var b=a;b.push(4);alert(a);//[1,2,3,4];

The above code is correct, but the following code is different.

Because B is assigned a value again, it does not point to.

In addition, parameters are similar to the scope of variables:

Comparison:

The preceding parameter is of the basic type, and only the value is passed in. The following parameter is of the reference type: (it also contains the case of re-assignment)

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.