JS pre-compilation, global variables, local variables related knowledge

Source: Internet
Author: User

1, undeclared variables are assigned, this variable is the global variable of the Window object all

Console.log (a);   A is not defined
a=10
Console.log (a);  10 equivalent to Console.log (WINDOW.A)

function Test () {
var a = b = 123;
}
Test ();
Console.log (b)  //123  
Console.log (a)   //a is not defined

2. All declared variables are properties of the Window object.

var aaa=111;
var bbb=222;

var ccc=333;

Equivalent to window{aaa:111,bbb:222,ccc:333}

3, pre-compilation occurs in the first moment of function execution, there are the following four steps:

(1), create AO (Activation object/Execution period context) objects;

(2), find formal parameter and variable declaration, the variable and formal parameter name is AO object attribute name, value is undefined;

(3), the integration of the actual parameters and formal parameters;

(4), in the function body found function declaration, the value assigned to the function body;

function fn (a) {
Console.log (a); //? A () {}

Console.log (a); 123
function A () {}
Console.log (a); 123
var B = function () {}
Console.log (b); //? () { }
}
fn (1);

JS pre-compilation, global variables, local variables related knowledge

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.