JavaScript Basics VI (memory release, scope destruction)

Source: Internet
Author: User

1. Heap Memory

Object data type or function data type in the definition of the first to open up a heap of memory, heap memory has a reference to the address, if there are variables equal to this address, we say that the memory is occupied, we can not destroy the

We want the heap memory to be freed/destroyed, as long as all variables referencing it are assigned null, and if nothing is occupied by the current heap memory, then the browser will destroy it when it is idle ...

2. Stack Memory

1) Global scope

The global scope is destroyed only when the page is closed

2) Private scope (only function execution produces a private scope)

In general, function execution forms a new private scope, and when the code in the private scope is completed, our current scope is actively freed and destroyed

However, there are special situations:

Some of the memory in the current private scope is occupied by something unexpected, so the current scope cannot be destroyed.

A. Function execution returns a value of the reference data type and is received outside of the function by something else, in which case generally the private scopes formed in this case are not destroyed

function fn () {             var num = +;              return function () {                              }         }         var f = fn ();   This private scope formed by the FN execution can no longer be destroyed.

B. Event binding methods for DOM elements in a private scope, in general our private scopes are not destroyed

var odiv = document.getElementById ("Div1"); ~function() {    odiv.onclick  function  () {   }}} (); // the private scope formed by the current self-executing function is also not destroyed

C.

function fn () {var num = +; return function () {}}  fn () (); // executes the FN first, returns the memory address of a small function, and then executes the returned small function without immediate destruction

JavaScript Basics VI (memory release, scope destruction)

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.