javascript: variables, for use, and memory issues

Source: Internet
Author: User

One: Understanding the values of the base type and reference type

  JavaScript variables have two different data type values: The base data type and the reference data type. The base data types are undefined, null, Boolean, number, string. Reference types are objects that are stored in memory, and JavaScript specifies that objects in memory cannot be manipulated directly, and when manipulating objects, they are actually references to objects that are manipulated. When assigning a value to a variable, you need to determine the type of the value, the actions performed on different types of variables are different, the reference type values can be created and deleted for the properties and methods, and the base data type is not available.

    1. Copy variables

For basic data types, when copying variables, re-open a memory, complex variable. The reference type, however, is simply a reference to the copied object.

2. Passing parameters

The values of all functions are passed by value.

3. Detection type

For basic data types, use typeof detection. For reference types using instanceof, the basic syntax is result = Variable instanceof constructor, and all reference types are instances of object objects.

Second: Understanding the implementation environment

  The execution environment is the most important concept of javascript, and the execution environment defines the other data that variables and functions have access to, and determines their respective behavior. There is a variable object associated with each execution environment, and all variables and functions defined in the environment are stored in the object.

The global execution environment is always a Window object, and all global variables and objects are saved in the Window object. Each function has its own execution environment, and when a function is executed, the environment of the function is pushed into the environment stack, and after the function is executed, the environment is ejected from the environment stack and the control is given to the superior environment. When code executes in an environment, a scope chain of variable objects is created, and the role of the scope chain is to guarantee orderly access to the variables and functions that are accessible in the environment. The front end of the scope is always the execution environment for the current code, the next level is the external environment that contains the current environment, and so on, and the last is always the global execution environment. Parsing of variables and functions is a one-level access search from the front-end of the scope to the back end.

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

1. Extending the scope chain

Some statements can temporarily add a variable at the front end of the scope, which is automatically deleted when the code finishes executing. There are two statements that enable extended scopes: Try-catch and with statements.

2. No block-level scope

In other languages, the code in curly braces has its own block-level scope, but there are no block-level scopes in JavaScript, and variables and functions defined in curly braces are added to the current execution environment, and all the variables defined inside curly braces can be accessed outside the curly braces.

Three: Understanding Garbage collection

  JavaScript has an automatic garbage collection mechanism that finds variables that are no longer being used and periodically recycles the memory at regular intervals. Make sure that JavaScript consumes less memory to optimize performance. When the data is no longer useful, set its value to NULL, release its reference, and leave it out of the execution environment to reclaim memory by the garbage collection mechanism, which applies to most global variables, and local variables are automatically reclaimed after the local environment has finished executing.

JavaScript: variables, for use, 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.