JavaScript Learning (ii): variables, scopes, and memory issues

Source: Internet
Author: User

Values for base types and reference types

  Basic type (simple data segment)

The base data type is accessed by value, because the actual value is stored in the variable.

  Reference types (objects that may consist of multiple values)

Reference types are objects that are held in memory, and JavaScript does not allow direct access to locations in memory. So it's actually manipulating the object's reference rather than the actual object.

1. Dynamic Properties

You can only add properties to a reference type value dynamically.

2. Copying variable values

Because the basic types and reference types are accessed differently, the variables are copied differently.

When a value of the base type is assigned to another variable from one variable, a new value is created and the value is copied to this location.

When the value of a reference type is assigned to another variable from one variable, the new value created is a pointer to the object, and the copy is actually a pointer, so two values will actually refer to the same object.

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 the local variable of the named parameter, so the two are unrelated, but have the same value.

When a value of a reference type is passed to a parameter, the address of the value in memory is copied to the local variable of the named parameter, so the change of the local variable is reflected outside the function, but does not mean that it is accessed by reference.

The proof is as follows:

function setName (obj) {    = ' Eric ';     New Object ();     = ' Cherry ';} var New Object (); SetName (person); alert (person.name)      ; // ' Eric '
4. Detection type

  typeof 

Variable instanceof constructor (returns True if the variable is an instance of the given reference type)

  Note: Because all reference types are object instances, it returns true if the reference type value and the object constructor are detected, and false is always returned if the base data type is used to detect.

Execution Environment and scope

  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 execution of the environment, the stack pops up its environment and gives control to the previous execution environment.

  When code executes in an environment, a scope chain of variable objects is created, guaranteeing an orderly access to all variables and functions that the execution environment has access to.

1. Extending the scope chain

When the execution flow enters any statement on the bevel, the scope chain is extended.

A catch block for the Try-catch statement; (Creates a new variable object that contains the declaration of the thrown Error object)

With statement. (The specified object is added to the scope chain)

2. No block-level scope

Variables defined in a conditional statement are added to the current execution environment, not just within a conditional statement.

  Declaring variables: Variables declared with Var are added to the closest environment. Variables that do not have a VAR declaration are added to the global environment.

Query identifier: The search process always starts backward from the front of the scope chain until the identifier is found.

Garbage collection 1. Mark Clear

Tag all variables stored in memory and clean them regularly.

2. Reference counting

Tracking the number of times each value is referenced, a variable with a reference count of 0 will have garbage collector cleanup. (however, because circular references cause references to never be zeroed, they are never recycled.) )

3. Performance issues

Dynamic allocation triggers the garbage collector's variable allocations, literals, and threshold values for array elements.

4. Managing Memory

Local variables are automatically contacted when they leave their execution environment.

The global variable is not, the best way is that once the data is no longer used, manually set its value to NULL to release its reference.

JavaScript Learning (ii): variables, scopes, 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.