JavaScript Learning notes--4. variables, scopes, and memory issues

Source: Internet
Author: User

  1. The ECMAScript variable may contain values for two different data types: the base type value and the reference type value, where the base type value is a simple data segment, whereas the reference type value refers to an object that may consist of multiple values;
  2. For 5 basic types of data: Undefined, null,boolean,number, and string, accessed by value, because the actual value stored in the variable can be manipulated;
  3. For reference-type values, JavaScript does not have direct access to the in-memory location, which means that the object's memory space cannot be manipulated directly, and when the object is manipulated, it is actually a reference to the object rather than the actual object.
  4. There are also differences when copying base type values and reference type values from one variable to another:
    • If the value of the base type is copied from one variable to another, a new value is created on the variable object, and the value is copied to the location assigned to the new variable (so the operation of the value of the newly created variable does not affect the original variable);
    • When a value of a reference type is copied from one variable to another, the value stored in the variable object is also copied into the space allocated for the new variable. In fact, a copy of this value is a pointer to an object stored in the heap, so the result is that two variables actually refer to the same object, so changing one of the variables will have the same effect on the other variable.
  5. The parameter passing of all functions in ECMAScript is passed by value. That is, the values outside the function are copied to the parameters inside the function. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference to a value of a type is the same as a copy of a reference type variable.
  6. Instanceof:result = variable instanceof constructor;
    • Alert (person instanceof Object); Variable person is object?
    • Alert (person instanceof Array); is the variable person an array?
  7. Scope chain: Ensures 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 of the environment in which the code is currently executing, and if the environment is a function, its active object (activation object) as the variable object. The active object initially contains only one variable, the arguments object. The next variable object comes from the containing (external) environment, and the next object comes from the next containment environment. In this way, it continues to the global execution environment; The variable object of the global execution environment is always the last object in the scope chain.
  8. Variables declared with Var are automatically added to the closest environment.
  9. Basic type values and reference type values have the following characteristics:
    • The base type value occupies a fixed amount of space in memory and is therefore stored in the stack memory;
    • A copy of this value is created by copying the value of the base type from one variable to another;
    • A reference type value is an object that is stored in the heap memory;
    • A variable that contains a reference type value does not actually contain the object itself, but rather a copy of the value that points to it;
    • Copying the value of a reference type from one variable to another is actually a pointer, so two variables end up pointing to the same object;
    • Determine which base type a value can use the TypeOf operator, and determine which reference type can use the instanceof operator,
  10. About the execution environment:
    • The execution environment has a global execution environment (also known as the Global Environment) and the function execution environment points;
    • Each time you enter a new execution environment, you create a scope chain that is used to search for variables and functions;
    • The local environment of a function not only has access to variables in the scope of the function, but also has access to its containing (parent) environment, and even to the global environment;
    • The global environment can only access variables and functions defined in the global environment, and cannot directly access any data in the local environment;
    • The execution environment of a variable helps determine when memory should be freed;
  11. Garbage collection routines about javascript:
    • The values that leave the scope are automatically marked for recycling and are therefore deleted during garbage collection;
    • "Mark Clear" is currently the mainstream garbage collection algorithm, the idea is to give the value of the current not used to tag, and then in the recovery of memory;
    • The reference count garbage collection algorithm is the number of times that all values are referenced, and the JavaScript engine no longer uses the algorithm;
    • Remove the reference to the variable (for example: var Globalvar = Createdvar ("things"); Globalvar = NULL;) helps eliminate the phenomenon of circular references and is beneficial for garbage collection. To ensure that memory is effectively recycled, you should be in touch with global objects, global object properties, and references to circular reference variables that are no longer in use.

JavaScript Learning notes--4. variables, scopes, and memory issues

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.