JavaScript Advanced Programming Learning Notes Chapter fourth-variables, scopes, and memory issues

Source: Internet
Author: User

1. A variable may contain values for two different data types: the base type value and the reference type value. A primitive type value refers to a simple data segment, whereas a reference type value refers to an object that may consist of multiple values.

2. Variable copy

    • If you copy the value of the base type 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, and two variables can participate in any operation without affecting each other.
    • 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. The difference is that a copy of this value is actually a pointer to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other variable.

3. Pass parameters: All the functions in ECMAScript are passed by value. 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.
4. Detection type: Use typeof when detecting basic data type, use instanceof operator when detecting reference type

5. Execution Environment: Defines the other data that variables or functions have access to, and determines their respective behavior. Each execution environment has a variable object associated with it (variable object), and all variables and functions defined in the environment are stored in the object. After all code in an execution environment is executed, the environment is destroyed, and all the variables and function definitions stored therein are destroyed.
6, each function has its own execution environment. When the execution flow enters a function, the environment of the function is pushed into an environment stack. After the function executes, the stack pops up its environment and returns control to the previous execution environment.

7. Contemporary code when executed in an environment, creates a scope chain of variable objects (scope chain). The purpose of a scope chain is to ensure 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 for the environment in which the code is currently executing.
8. Two ways to extend the scope chain: Some statements can temporarily add a variable object to the front of the scope chain, and the variable object is removed after the code executes.

    • Catch block for Try-catch statements
    • With statement

9. There is no block-level scope, especially when using a for statement, which is most confusing.

10. Query identifier process: When you reference an identifier in an environment for reading or writing, you must search to determine what the identifier actually represents. The search process starts at the front end of the scope chain and queries the identifiers that match the given name upwards. If the identifier is found in the local environment, the search process stops and the variable is ready. If the variable name is not found in the local environment, it continues to search up the scope chain. The search process goes back to the variable object of the global environment. If the identifier is not found in the global environment, it means that the variable is not yet declared.
11. The principle of garbage collection: Identify variables that are no longer in use, and then release the memory they occupy. To do this, the garbage collector periodically performs this operation at a fixed time interval (or a predetermined collection time in code execution).

    • The most common method of garbage collection in JavaScript is tag cleanup. When a variable enters the environment (for example, when declaring a variable in a function), the variable is marked as "entering the environment." When a variable leaves the environment, it
      Mark as "Leave the environment". The rubbish collector adds tags to all variables stored in memory at run time (of course, you can use any markup method). It then removes the variables in the environment and the tags of the variables referenced by the variables in the environment. Variables that are tagged later will be considered as variables to be deleted because variables in the environment are inaccessible to those variables. Finally, the garbage collector completes the memory cleanup work, destroys those tagged values, and reclaims the memory space that they occupy.
    • Reference count: Keeps track of the number of times each value is referenced, and the garbage collector frees memory that is consumed by values that are referenced to zero at run time.
    • The reference count algorithm causes problems when there is a circular reference phenomenon in your code.
    • Removing a reference to a variable not only helps eliminate circular references, but also benefits garbage collection. To ensure effective return to the
      The global objects, global object properties, and circular reference variables that are no longer in use should be released in a timely manner.

JavaScript Advanced Programming Learning Notes Chapter fourth-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.