Chapter fourth variables, scopes, and memory issues

Source: Internet
Author: User

The nature of the loosely typed JavaScript variable determines just one name that is used to hold a particular value at a specific time. Because there is no rule that defines what data type values A variable must hold, the value of the variable and its data type can change over the lifetime of the script.

4.1 Values for base types and reference types

The ECMAScript variable may contain values for two different data types: The base type refers to simple data segments, whereas reference types refer to objects that may consist of multiple values.

5 Basic data types (Undefined, Null, Boolean, number, String) are accessed by value, because the actual values saved in the variable can be manipulated. The base type value occupies a fixed amount of space in memory and is therefore stored in the stack memory.

The value of a reference type is an object that is held in memory, and JS cannot manipulate the object's memory space directly, but is actually a reference to the object in action instead of the actual object.

4.1.1 Properties of the dynamic

Define the way: the difference u that build a variable and assign a value to the variable.

For a value of a reference type, you can add properties and methods to it, and you can change and delete its properties and methods, but you cannot add properties to the value of the base type, that is, you can add properties dynamically to the reference type for future use.

4.1.2 Copy variable values

If you copy the value of the base type from one variable to another, a new value (copy) is created on the variable object, and the value is copied to the location assigned to the new variable. These two variables can participate in any operation without affecting each other.

The value of a reference type is an object that is saved in heap memory. When a value of a reference type is assigned to another variable from one variable, the value stored in the variable object is also copied into the space allocated for the new variable. A copy of this value is actually a pointer to an object in heap memory. At the end of the copy operation, two variables will actually refer to the same object, changing one of the variables, affecting the other variable.

4.1.3 Passing parameters

The parameters of all functions in ECMAScript are passed by value.

When you pass a value of a primitive type to a parameter, the passed value is copied to a local variable (that is, a named parameter, which is an element in the arguments object). When you want to pass a value of a reference type to a parameter, the address of the value in memory is copied to a local variable, so the change of the local variable is reflected outside the function. Even though the value of the parameter is modified inside the function, the original reference remains unchanged.

4.1.4 Detection Type

Detecting basic data Types typeof

To detect the value of a reference type:

result = Variable instanceof constructor

4.2 Execution Environment and scope

The execution environment defines the other data that a variable or function has access to, determines their behavior, and determines the life cycle of the variable. All variables and functions defined in the environment are stored in the variable object associated with it.

The global execution environment is one of the outermost execution environments.

Each function has its own execution environment.

When code executes in an environment, it creates a scope chain of variables that is used to guarantee orderly access to all variables and functions that the execution environment has access to.

The internal environment can access all external environments through the scope chain, but the external environment cannot access any variables and functions in the internal environment.

The execution environment of a variable helps determine when memory should be freed.

4.2.1 Extending the scope chain

Adds a variable object to the front of the scope chain.

The catch block of the 1.try-catch statement creates a new variable object that contains the declaration of the thrown error object.

2.with statement, the specified object is added to the scope chain.

4.2.2 No block level scope 1. Declaring variables

Variables declared with Var are automatically added to the closest environment. If you initialize a variable without using the var declaration, the variable is automatically added to the global environment.

Before initializing a variable, be sure to declare it first.

2. Query identifiers

When referencing an identifier for reading or writing, you must search to determine what the identifier actually represents. The search process starts at the front of the scope chain, queries the identifier that matches the given name up and down, finds it to stop, or continues to search up the scope chain, which means that the variable is not yet declared.

4.3 Garbage collection

The execution environment is responsible for managing the memory used during code execution.

The principle of the garbage collection mechanism: Identify variables that are no longer in use, and then release the memory they occupy. The garbage collector periodically performs this operation at regular intervals. Leaving the scope will be automatically tagged for recycling.

4.3.1 Mark Clear

is the current mainstream garbage collection algorithm that adds tags to values that are not currently in use, and then reclaims their memory.

The garbage collector adds tags to all variables stored in memory at run time. The variables in the environment and the tags of the variables referenced by the variables in the environment are then removed. After that, it is added that the tag variable is considered a variable to be deleted, and finally the memory is cleared.

4.3.2 Reference count

Trace records the number of times each value is referenced. There is a serious problem: circular references can cause large amounts of memory to not be recycled.

4.3.3 Performance Issues

Determines the time interval for the garbage collector.

4.3.4 Managing Memory

The amount of available memory allocated to a Web browser is typically less than that allocated to the desktop application, and the intent is that the way memory exhaustion causes the system to crash. Memory limits affect the allocation of memory to variables, and also affect the call stack and the number of statements that can be executed concurrently in a thread. So make sure that you use the least amount of memory to get better performance on your page.

The best way to optimize memory consumption is to save only the necessary data for the code in execution.

If the data is no longer useful, set its value to NULL to release the reference, that is, dereference. Applies to most global variables and global object properties. The references to the global variables, global object properties, and circular reference variables that are no longer used should be lifted in time.

Chapter fourth variables, scope, and memory issues

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.