Basic Knowledge Review--JS scope

Source: Internet
Author: User

1. Understanding of the JS scope.

Scope

Scopes are divided into global scopes and function scopes, which we can understand as variables ' living environment (space). The global scope contains the function scope, and variables in the scope of the function can access variables in the global scope, but vice versa.

Variable Promotion

There is also the phenomenon of variable promotion in the JS scope (only variables declared by VAR will have a variable promotion, not window Declaration), and the assignment statement will take effect at last. When I define a variable, if it is not assigned a value, it is belonging to undefined;

Priority of variable Promotion

function declarations > function parameters (function parameters belong to function scope;) > Custom variables

Extend scope, Closure (return function () {alert (...);} )

Closure Core idea: When the function is called, the function is declared, and it is looked out through the first layer of the scope;

  

1)

WINDOW.A = 1;

var a = 2; Global scope

function Test () {

var a = 3; function scope

alert (a);

}

alert (a);

As shown above, the order of the A variable in alert (a) is the function scope-global scope, found inside the test function, the popup value is 3, the second alert, in the global scope, looks for a, the popup value is 2;

2)

var a = 2;

function Test2 () {

var A;

alert (a);

var a = 4;

}

For the inside of the test function, there is a declared variable a, but the assignment of a is followed by the alert function. Consider that when executing a function, all the variables used inside the function are declared first on the function's head and are not assigned a value. So the popup value is undifined;

3)

function Test3 (a) {

function A () {}; Also promoted to the function head of the variable, because the variable name is the same, according to the priority of the relationship, alert pop-up value is function a function body;

A = 7;

var A;

alert (a);

var a = 5;

function A () {

}

}

TEST3 (7);

4)

var obj = {

Name: ' Aoao ',

Getname:function () {

alert (this.name);

}

}

Obj.getname (); 1

var test5 = Obj.getname;

Test5 ();//2

Note the difference between callout 1 and callout 2.

For callout 1: The function getname is declared inside obj, which is called when this points to the object obj;

For callout 2: The function body assignment of the function GetName gives the TEST5 this variable, which is equivalent to:

var test5 = function () {

alert (this.name);

}

Attention! The function is declared under the Window object, this points to the Window object;

5) (The difference between declaring a function and an expression of a function)

Function name () {alert (3);};

Name ();

Function name () {alert (3);}

And

var name;

Name2 ();

var name2 = function () {alert (2);};

Declaring a function puts the entire function declaration to the head, and the function expression promotes the variable of its function name to the head, so it is safer to use declarative functions;

Basic Knowledge Review--JS scope

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.