Chapter fourth variable scope and memory issues

Source: Internet
Author: User

4.1 Variables

Variables contain 2 types of values:

the value of the base type:   that is Undefined,null, String,number,boolean. by value access, you can manipulate the actual values that are saved in the variable, and you cannot add properties.

var name = "Nicholas"=console.log (name.age); // undefined

value of the reference type: An object that consists of multiple values. JS does not allow direct manipulation of the object's memory space. To add a property to an object is to add it to the actual object.

Differences between the two types of values:

1. Save in different ways: the basic type is saved in the object space. The reference type is saved in the memory again.

2. When copying a variable value: The base type is copied from one variable to another, the variable object creates a new value, and then copies the value to the new variable.

A pointer to a reference type copy value, stored in a new variable, actually referencing the same object.

var New Object (); var obj2 == "Nicholas"; Console.log (obj1); Console.log (OBJ2) ; // Object {name: "Nicholas"} // Object {name: "Nicholas"}

Passing Parameters

The parameters of all functions in JS are passed by value.

The value of the base type is copied from one variable to another, and the value is actually copied in two copies.

function Addten (num) {   +=10;    return num;} var count =; var rezult = Addten (count); Console.log (Rezult); Console.log (count) ; // the  value saved by the Rezult is passed as a parameter into the Addten 10, and then the return //  count is  outside, not affected by +10, So it returns 20. It's good to prove that the value of the base type stored in the variable copies the value to a different replica that creates a copy

When a parameter is an object (an object is one of the reference types), it is also passed by value.

function setName (obj) {   = "Nicholas";    New Object ();    = "Gray";  } var New Object (); SetName (person); Console.log (Person.name) ; // Nicholas The  original reference remains unchanged even if the value of the parameter is modified inside the function.
When the function internally overrides obj, the variable refers to a local object, which is destroyed immediately after the function is executed.

Detection Type

instanceof: The variable is returned true for the instance of the given type.

function setName (obj) {   = "Nicholas";  } var New  instanceof  Object); // true person is an instance of object
Execution Environment and scope

execution Environment: defines the other data that variables or functions have access to, and determines their respective behavior. After execution of the code in the execution environment, the environment is destroyed, and the variables and functions are destroyed.

Variable object: Each execution environment has a variable object. All variables and functions defined in the environment are saved in this object.

Global Execution Environment: One of the outermost execution environments, also considered a Window object. All global variables and functions are created as properties and methods of the Window object.

scope chain: The function has its own execution environment. When code executes in an environment, it creates a scope chain for the variable object. The purpose is to search for variables and functions. The front end of the scope chain is always the variable object of the environment in which the current execution code resides.

var color = "Blue"; function ChangeColor () {    if(color = = = "Blue")        {= "red";       Else {          = "Blue";        }     } ChangeColor (); Console.log ("color is now" + color);            // Color is now red

The scope chain of a function ChangeColor consists of 2 objects, its own variable object (arguments object), and the global variable object (color).

var color = "Blue"function  changecolor () {   var anothercolor = "Red" ;       function swapcolors () {    var tempcolor = anothercolor;     = color;     = tempcolor;    }}

The internal environment can access all external environments through the scope chain, but the external environment cannot access the variables and functions of the internal environment.

no block-level scopes

In JS, the variable declaration in the IF statement adds the variable to the current execution environment.

if (true) {  = "Red";} Console.log (color); // Red

The For statement creates a variable i since the For loop execution ends, there is still a loop outside the execution environment.

 for (var i=0; I <10; i++) {        i+ +;    } Console.log (i); // Ten

Query identifiers

The query identifier starts at the front of the scope and looks up the identifier that matches the given name. Until it is found, it is not found to imply that the variable is not declared.

var color = "Blue"; function GetColor () {    return color;                // first, look for the color identifier in the GetColor function, not found                                  // then up, the parcel getColor is the global execution environment, finally in the global execution environment, and immediately returns   } console.log (GetColor ()); // Blue

Local variables have the highest precedence if there is an identifier with the same name.

var color = "Blue"; function GetColor () {    var color = "Red";      // Local variables override global variables    return color;   } Console.log (GetColor ()); // Red

Chapter fourth variable scope and memory issues

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.