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)