About JS pre-interpretation of the relevant understanding _ basic knowledge

Source: Internet
Author: User
Tags anonymous function definition

1, JS in the memory space is divided into two types: stack memory, heap memory

Stack Memory: Provides the environment for JS Code execution, and stores the value of the base data type; -> global scope or private scope is actually stack memory

Heap Memory: A value that stores reference data types (objects are stored in attribute names and property values, which are stored as strings in the function body)

2, when the browser loads our HTML page, first will provide a JS code execution Environment-> global scope (Global->window)

3, in the JS code before the implementation, the browser also needs to do some things by themselves: to all with var/function keyword in advance of the declaration or definition-> "pre-explained" (Variable voice)

Statement (declare)-> told the browser I have such a thing, such as Var num1; function fn;

Definition (defined)-> assign values to variables or functions that we declare, such as num1=12; Fn=function () {}

The [important] variable declares no definition, and the default value is undefined (undefined)

4. var and function are handled differently during the pre-interpretation phase

var-> only declares this variable in advance when it is explained, and the assignment is done only when the code executes

function-> in advance of the definition of the declaration will be completed (at the time of code execution encountered the definition of the code directly skipped)

[Important] just in the beginning of the window for the interpretation of the FN function is currently stored in the string, so var total has no practical significance, so do not do a pre-interpretation-> "pre-interpretation is occurring under the current scope of the"

Console.log (obj);//->undefined
 var obj = {Name: "Zhang says", age:10};
 function fn (NUM1, num2) {//code is skipped directly when executing to this line, because we have completed the declaration and defined the
var total = num1 + num2 in the pre-explained time;
 Console.log (total);
 }
 var num1 =;

 FN (NUM1, 100);//execute FN, assign value of global variable NUM1 to formal parameter num1, assign 100 to formal parameter num2

5. Variables declared under global scope are global variables

A variable declared in a private scope is a private variable; a function's formal parameter is also a private variable;

How can I tell if a variable that appears in a function is private or global?

First look at whether the formal parameters, then see if it is declared in a private scope (there is no Var), one of which is a private variable, then in the current function no matter where the appearance is private, and the overall situation is not half a dime of the relationship; Then go to the top-level scope to find ...

6, function execution will form a new private scope (stack memory) for the function body code execution;

1) assigning values to formal parameters
2 The pre-interpretation under private scopes
3 code execution under a private scope

The new private scope of formation also protects the inside private variables from the outside world, and we-> this protection mechanism of the function "closure

Difference: A statement with Var that can be declared before the code executes without the assertion that the Var is not in advance

1, regardless of whether the conditions are established to be a preliminary explanation

Window Pre-explained: Var A; -> WINDOW.A;
 if (! () A ' in window ') {//"a" in window-> true
 var a = "we";
 }
Console.log (a);//->undefined

2, the preliminary interpretation only occurs in the "=" left, only to the left of the preliminary explanation, the right is the value is not to be explained

function expression of an anonymous function: An event that assigns the part of a function definition as a value to a variable or element

Pre-explained time: VAR fn; The default value for->FN is undefined

fn ();//->undefined () uncaught TYPEERROR:FN is not a function JS only functions can be executed && js the above code if the error, The following code does not execute the
var fn = function () {
 console.log ("OK") without
 any special handling;
 fn ();

Pre-explained: fn=xxxfff000
 fn ();//-> "OK"
 function fn () {
 console.log ("OK");
 }
 fn ();//-> "OK"

3, function body return the following code is not in execution, but the following code needs to participate in the preliminary explanation, and the back of the thing is to be processed, but because it is returned as a value, so do not make a preliminary explanation;

var total =;
 function fn () {
 console.log (total);
 return function sum () {};//return Returns the value in the function to the outside of the function, where the corresponding memory address of the functions is returned to the outside of the function, for example: return xxxfff111; The code in the function body is not executing the var total
=
 FN ();

4, the function of anonymous functions in the global scope is not to be explained in advance

Self-executing functions for anonymous functions: definition and execution completed together

(function (num) {}) (100);

5, in the interpretation of the time, if you encounter a duplicate name, only once, do not repeat the declaration, but the assignment is to repeat the

In JS, the name of the variable and the name of the function are counted as duplicates.

Pre-explained:

VAR fn; Declaration
fn = xxxfff000; [statement] no + definition
fn = xxxfff111; [Declaration] No + define
->fn=xxxfff111
 var fn = 12;//window.fn=12
 function fn () {//window.fn=function () {}
 }
 function fn () {
 }

The above is a small series for you to bring the relevant understanding of JS interpretation of all the content, I hope that we support cloud-Habitat Community ~

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.