Summary of variables, scopes, and memory issues [JavaScript]

Source: Internet
Author: User

JavaScript's basic type values and reference type values have the following characteristics;

1, the basic type value in memory occupies a fixed size, so it is stored in the stack memory;

2, the reference type value is an object, stored in the heap memory;

3. Copy the value of the base type from the variable to another variable, and a copy of the value is created;

4. A variable containing a value of a reference type actually contains not the object itself, but a pointer to that object;

5. Copy the value of the reference type from one variable to another, and the copy is actually a pointer, so two variables are ultimately pointed to with an object;

6. Determine which base type a value can use the TypeOf operator, and determine which reference type can use the instanceof operator.

A few summaries of JavaScript's execution Environment:

1, the execution environment has a global execution environment and function execution environment points;

2. Each time a new execution environment is entered, a scope chain for searching variables and functions is created;

3, the local environment of the function not only has access to variables in the scope of the function, but also has access to its containing (parent) environment and even the global environment;

4, the Global environment can only access variables and functions defined in the global environment, but not directly access any data in the local environment;

5. The execution environment of a variable helps determine when memory should be freed.

A few summary of JavaScript garbage collection:

1. The value of the departure scope is automatically marked as recyclable and therefore will be deleted during garbage collection;

2, "Mark Clear" is the current mainstream garbage collection algorithm, the idea of this algorithm is to add tags to the current value, and then reclaim its memory;

3, another garbage collection algorithm is "reference count", the idea of this algorithm is to keep track of the number of times all values are referenced. The JavaScript engine is no longer using this algorithm, but this algorithm can still cause problems when accessing non-native JavaScript objects such as DOM elements in IE.

4, the "Reference counting" algorithm can cause problems when there is a cyclic reference in the current code.

5, the release of the reference to the variable not only help to eliminate the phenomenon of circular reference, but also for garbage collection benefits. In order to ensure efficient recovery of memory, it should be timely to release references to global objects, global object properties, and circular reference variables that are no longer in use.

If the above description is wrong, I hope you will make suggestions, thank you

Summary of variables, scopes, and memory issues [JavaScript]

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.