JavaScript-variables, scopes, garbage collection

Source: Internet
Author: User

1. Variables

There are basic types and reference types, and the two types are defined in a similar way (var X = XXX), with a large difference in operation (the reference type can be deleted and changed to its properties and methods).

eg

var j= new Object ();

J.name = "hehe";

alert (j.name); Oh

var q = "hehe";

Q.K = "Jqk";

alert (Q.K); Undefined

With respect to replication, reference types are just copy references.

With regard to arguments, the transfer of a primitive type value is like a copy of a primitive type variable, and the passing of a reference type value is like a copy of a reference type variable . you can think of the ECMAScript parameter as a local variable .

function SetName (obj) {

Obj.name = "Name1";

obj = new Objact ();

Obj.name = "Name2";

}

var person = new Object ();

SetName (person);

alert (person.name); "Name1"

Using typeof and Instanceof detection types

2. Execution environment and scope

Execution Environment: The execution environment defines the other data that a variable or function has access to, determines their behavior, and each execution environment has a variable object associated with it, and all variables and functions defined in the environment are stored in the object.

The global execution environment is one of the outermost execution environments, and the objects that represent the execution environment are different depending on the hosting environment in which the ECMAScript implementation resides. When the code executes in an execution environment, the environment is destroyed and its variables and functions are destroyed. The global execution environment is not destroyed until the application exits.

The global execution environment in a Web browser is a window object, so all global variables and functions are properties and methods of the Window object. The Global Execution Environment (window) is destroyed when the Web page or browser is closed.

JavaScript does not have a block-level scope, that is, if, the variables in the for block are still accessible at the curly brace peers,

eg

if (true) var i = 1;

alert (i); 1

A variable that is not declared becomes a global variable. Non-declared variables are not allowed in strict mode.

3. Garbage collection

There are typically two policies: Mark Purge and reference count

Tag cleanup is marked as "go to Environment" when a variable enters the environment, and is marked as "out of the environment" when it leaves the environment, and is cleared when garbage collection is marked as out of the environment. Now the JavaScript implementations of IE, Firefox, Chrome, Safari, and opera all use the tag-clear garbage collection strategy.

The reference count is the number of times each value is referenced. If a variable is declared and assigned a reference type value, the reference count is 1, and if the variable that contains the value references another value, the value is reduced by 1. When the number of references is 0 o'clock, it can be used to reclaim memory. However, there is a serious problem with reference counting: circular referencing.

Before IE9, the objects in the BOM and Dom of IE were implemented in the form of COM objects using C + +, and the garbage collection mechanism of COM objects used reference counting policies. Therefore, even if the JavaScript engine is implemented with a tag cleanup policy, there is a circular reference problem that causes a memory leak if the COM object is involved in IE.

To ensure that less memory is used to get better performance for the page, it is a good idea to manually set it to null to free the reference once the data is no longer useful (dereference is dismissed).

JavaScript-variables, scopes, garbage collection

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.