4. Javacript advanced Programming-variables, scopes, and memory issues

Source: Internet
Author: User

1.1 Values for base types and reference types

A variable contains values for two different data types: a base type value and a reference type value. A primitive type value is a simple data segment, an object that refers to a value of a type that can consist of multiple values

1.1.1 Properties of the dynamic

For the value of a reference object, you can add properties and methods to it dynamically

var person = new Object ();

Person.name = "Chuck";

alert (person.name); Chuck

1.1.2 Copy Variable values

(1). Basic types of variable replication

var num = 5;

var num2 = num;

Before copying:

Num

5 (number Type)

After copying:

Num2

5 (number Type)

Num

5 (number Type)

(2). Variable copy of a reference type

var obj1= new Object ();

var obj2=obj1;

Obj1.name = "Chuck";

Alert (obj2.name)//chuck

1.1.3 Passing parameters

Parameter passing is consistent with replication, and the value of the variable is copied to the specified parameter

1.1.4 Detection Type

Basic types Use typeof check

Reference types use instanceof, with the following syntax:

result = Variable instanceof constructor

For example:

Person instanceof Object

Colors instanceof Array

Pattern instanceof RegExp

1.2 Execution Environment and scope

The 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, and all variables and functions defined in the environment are stored in the object.

The global execution environment is an execution function on the perimeter, and in a Web browser, the global execution environment is considered a Window object because all global variables and functions are created as properties and methods of the Window object.

Each function has its own execution environment, and when the execution flow enters a function, the environment of the function is pushed into an environment stack, and when the environment is executed, the stack pops its environment and returns control to the previous execution function.

When code executes in an environment, a scope chain is created, and the front end of the scope chain is always the variable object of the environment in which the code is currently executing. If the environment is a function, its active object is used as a variable object. The active object initially contains only one variable, the arguments object, the object in the scope chain from the external environment, and the next variable object from the next containing environment, which continues to the global variable, and the execution environment of the global variable is always the last object in the scope chain.

Objects in the scope chain, the internal environment can access the external environment through the scope chain, the external environment cannot access the internal environment

var color = "Blue";

function Changecolr () {

var anothercolor = "Red";

function Swapcolor () {

var tempcolor = Anothercolor;

Anothercolor = color;

color = Tempcolor;

Here you can access color, anothercolor, and Tempcolor

}

Here you can access color, Anothercolor, but not access Tempcolor

Swapcolor ();

}

Only color can be accessed here

CHANGECOLR ();

1.3 Garbage collection

JavaScript has an automatic garbage collection mechanism

(1). The value leaving the scope is automatically marked as recyclable and will be deleted during garbage collection

(2). "Mark Clear" is the current mainstream garbage collection algorithm

(3). Another garbage collection algorithm is a "reference count"

(4). Removing variables not only eliminates circular references, but also benefits garbage collection

4. Javacript advanced Programming-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.