And the scope chain execution environment Detailed examples

Execution Environment:

Execution Environment (execution context, for simplicity, sometimes referred to as "Environment") is one of the most important JavaScript probability
concept. Execution environment defines data variables or other functions have access to, determine their own behavior. Each execution environment has a
variable associated with an object (variable object).

Global environment:

Global execution environment is the most peripheral of an execution environment. Depending on the ECMAScript implementation where the host environment, that the implementation of ring
target environment are not the same. In a Web browser, global execution environment is considered to be the window object .

 

 

Scope destruction:

All code is an execution environment to perform complete
after completion, the environment is destroyed, save where all variable and function definitions also will be destroyed (global execution environment until the application back
out - for example, close the page or browser - when will be destroyed).

Execution Environment mechanisms:

Each function has its own execution environment. When the stream enters performing a function, the function will be pushed into the ambient environment of a stack.
And after the function executes, the stack will pop up its environment, returning control to the execution environment before. ECMAScript program execution flow
is precisely controlled by the convenience of this mechanism.

Scope chain:

When the code is executed in an environment, it will create a variable object scope chain (scope chain). Use the scope chain, it is
to ensure orderly access to all variables and functions have access to the execution environment . The front of the scope chain, currently executing code that is always the
object variable environment. If this environment is a function, it is the active object (activation object) as a variable object. Activities
as at the beginning contains only one variable, namely the arguments object (the object in the global environment does not exist). Scope chain
next variable from the object comprises (external) environment, and then the next object is a variable comprising the environment from the next. In this way, it has been extended
continuously to the global execution environment; variable object of the global execution environment of the scope chain is always the last object .
A search identifier is identifier resolution along scope chain stage by stage process. Search process always starts from the front of the scope chain,
then back stepwise backward until you find the identifier (if identifier is not found, usually results in an error).

Scope example:

var Color = "Blue" ;
 function changeColor (NUM) {
   var anotherColor = "Red" ;
   function swapColors () {
   var tempColor = anotherColor; 
  anotherColor = Color; 
  Color = tempColor;
   // where access to color, anotherColor, num and tempColor 
  }
 // here accessible color, num and anotherColor, but can not access tempColor 
  swapColors (); 
} 
// here only access Color 
changeColor (88);

Code above involving a total of three execution environments: the global environment, changeColor () the local environment and swapColors () of the local
environment. The global environment has a variable color and a function changeColor (). changeColor () in the local environment has
a variable name and a function name anotherColor swapColors (), but it can also access the global environment becomes
amount color. swapColors () in the local environment has a variable tempColor, the variable can only be accessed in this environment.
Whether the global environment or changeColor () will not have access to the local environment tempColor. However, in swapColors () inside
, you can access all of the other two variables in the environment, because that environment is its two parent execution environment .

js not block-level scope:

if (true) {
  var color = "blue";
}
alert(color); //"blue"

for (var i=0; i < 10; i++){
  doSomething(i);
}
alert(i); //10

      In JavaScript, if a variable declaration statement variables will be added to the current execution environment (in this case
the global environment).

   Variable created by the statement for i even after the end of the cycle for execution, it will still exist

The external loop execution environment.

Undeclared variables:

Variables declared using var will automatically be added to the closest environment. Inside the function, the closest environment is a function of the local
environment; in with the statement, the closest environment is a function of the environment. If you do not use the var statement to initialize a variable, the variable will automatically
move to be added to the global environment.

function add(num1, num2) {
  sum = num1 + num2;
  return sum;
}
var result = add(10, 20); //30
alert(sum); //30

Variable sum in this example does not use the var keyword is initialized assignment. Accordingly, when the End call the Add (), adding
the variable sum is added to the global environment will continue to exist; even if the function has completed execution, the code behind it can still access. In strict mode will lead to error, it is recommended variables to be declared.

 

to sum up:

  1.      Basic types of fixed-size value occupies space in memory, so the stack is stored in the memory;
  2.  Copy the value of the basic types of variables from one variable to another, you create a copy of this value;
  3.  reference value type of the object is stored in the heap memory;
  4.  variable type contains a reference to the object is not actually contain the value itself, but a pointer to the object;
  5.  copied from one variable to another type of a reference variable value, copying is actually a pointer, and therefore the final two variables point to the same object;
  6.       Determining a value which is substantially typeof operator type can be used, which is determining the value of a reference type can be used instanceof operator.
  7.       All variables (including primitive types and reference types) are present in an execution environment (also known as scope) among the execution environment determines the life cycle of variables, and which part of the code which can be accessed variable.
 

Here are a few summary of the execution environment:

  1.  execution environment global execution environment and the execution environment of the function points (also referred to as global environment);
  2.  each time you enter a new execution environment, will create a search for variables and functions of the scope chain;
  3.  function of the local environment not only have access to the function scope variables, and have access to it contains (parent) environment, as well as the global environment;
  4.  only access the global environment variables and functions defined in the global environment, but can not access any data directly to the local environment;
  5.  execution environment variables help determine when it should free up memory.
 

JavaScript is a programming language with automatic garbage collection mechanism, developers do not have to worry about memory allocation and recovery issues. We can make the following summary of JavaScript's garbage collection routine.

  1.  value out of scope will be automatically marked as recyclable, and therefore will be deleted during garbage collection.
  2.  "Clear mark" is the mainstream garbage collection algorithm, the idea of ​​this algorithm is not currently used to value plus the mark, and then recover its memory.
  3.  Another garbage collection algorithm is "reference counting" idea of ​​this algorithm is to keep track of the number of all values ​​referenced. JavaScript engines are currently not using this algorithm; but when accessing non-native JavaScript object (such as a DOM element) in IE, this algorithm may still cause problems.
  4.  When the code in the presence of a circular reference phenomenon, "reference counting" algorithm can cause problems.
  5.  lift variable references not only help to eliminate the phenomenon of circular references, but also good for garbage collection. In order to ensure effective recovery of memory, it should be lifted global objects no longer in use, references the global object properties and variables of a circular reference.

 

Guess you like

Origin www.cnblogs.com/LeeeOooonHuang/p/11498951.html
Recommended