variables, scopes, and memory issues in JavaScript (JavaScript Advanced Programming Chapter 4th)

Source: Internet
Author: User

One, variable

(1) ECMAScript variables Ken can contain values for two different data types: primitive Type value and reference type value . A primitive type value refers to a simple piece of data that refers to an object that may consist of multiple values.

(2) The basic data type is access by value, you can manipulate the actual value that is saved in the variable, the value of the reference type is saved in an in-memory object, the manipulation object is actually a reference to an object in action instead of the actual object, and the value of the reference type is accessed by reference.

(3) passing parameters. The parameters of all functions in Ecmscript are passed by value.

function setName (obj) {     = "Nicholas";      New Object ();   Understood as a new object, this object differs from the value of the parameter obj, that is, the address in the heap is different, the values in the heap are different, not the same object      obj.name = "Greg";   When the function finishes executing, the new obj is destroyed. }var person = new Object (), setName (person), alert (person.name)   ; "Nicholas"

When you override obj inside a function, this variable refers to a local object. This local object is destroyed immediately after the function is executed.

(4) instanceof operator If a variable is given an instance of a reference type, the instanceof operator always returns true, and if the value of the base type is detected using the instanceof operator, the operator always returns false, because the base type is not an object.

II. implementation Environment and scope

(1) The implementation environment is one of the most important concepts in JavaScript. The execution environment defines the other data that a variable or function has access to, and determines their respective behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object.

The global execution environment is one of the outermost execution environments. In a Web browser, the global execution environment is considered a Window object.

When code executes in an environment, a scope chain of variable objects is created. The role of a scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. The front end of the scope chain, which is always the variable object for the environment in which the code is currently executing. If the environment is a function, its active object is used as a variable object. The active object initially contains only one variable, the arguments object (this object does not exist in the global environment). The variable object for the global execution environment is always the last object in the scope chain.

(2) No block level scope

if (true) {       var color = "Blue";} alert (color);    // Blue;  for (vari = 0; i < 10;i++) {  dosomething (i);} alert (i);    // Ten;

For JavaScript, the variable I created by the for statement will still exist in the execution environment outside the loop even after the for loop execution has ended.

Third, garbage collection

(1) JavaScript has an automatic garbage collection mechanism. The rationale for this garbage collection mechanism is simple: identify variables that are no longer in use, and then release the memory they occupy.

The most common method of garbage collection in JavaScript is tag cleanup . The garbage collector adds tags to all variables stored in memory when it is running. It then removes the variables in the environment and the tags of the variables referenced by the variables in the environment. The variable that is tagged after this is considered a variable to be deleted.

Another less common garbage collection strategy is called reference counting.

(2) Managing memory

The problem with JavaScript in memory management and garbage collection is a bit different. One of the main problems is that the amount of available memory allocated to a Web browser is typically less than that allocated to a desktop application. This is done primarily for security reasons, to prevent a Web page running JavaScript from exhausting all of the system memory and causing the system to crash. Memory throttling issues not only affect allocating memory to variables, but also affect the call stack and the number of statements that can be executed concurrently in one thread.

The best way to optimize memory consumption is to save the necessary data for code values in execution. Once the data is no longer useful, it is best to release its reference by setting its value to null-this is called dereferencing

。 This practice makes use of properties for most global variables and global objects.

function Createperson (name) {     varnew  Object ();      = name;      return Localperson;} var globalperson = Createperson ("Nicholas"); // manually de-Globalperson references null;

Lifting a reference to a value does not mean that the memory occupied by the value is automatically waved. The real effect of dereferencing is to leave the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs.

variables, scopes, and memory issues in JavaScript (JavaScript Advanced Programming Chapter 4th)

Related Article

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.