JavaScript Advanced Programming: Fourth Chapter

Source: Internet
Author: User

variables, scopes, and memory issues

A 1.ECMAScript variable may contain values for two different data types: a base type value and a reference type value. A primitive type value refers to a simple data segment, and a reference type value refers to an object that consists of multiple values.

2. Dynamic properties: A method that defines a primitive type value and a reference type value is similar: Creates a variable and assigns a value to the variable. However, when this value is saved to a variable, the operation of the different type values can be performed in large-diameter chambers. For values of reference types, we can add properties and methods to them, and you can change and delete their properties and methods. Here's an example:

var person = new Object ();

Person.name = "Nicholas";

alert (person.name);//"Nicholas"

The above code creates an object and saves it in the variable person. We then added a property named name to the object. This property is always there if the object is not destroyed or if the property is not deleted.

However, we cannot add properties to the values of the base type, although doing so does not result in any errors. Like what:

var name = "Nicholas";

Name.age = 27;

alert (name.age);//undefined

3. Copy the variable value:

When a value of the base type is copied from one variable to another, a new value is created on the variable object, and the value is copied to the location assigned to the new variable. Take a look at an example:

var num1 = 5;

var num2 = NUM1;

The values for both NUM1 and num2 are 5, but their operations are independent of each other.

When a reference type value is copied from one variable to another, the value stored in the variable object is also copied into the space allocated for the new variable. The difference is that a copy of this value is actually a pointer to an object stored in the heap. When the copy operation is finished, two variables will actually refer to the same object. Therefore, changing one of the variables will affect the other, as shown in the following example:

var obj1 = new Object ();

var obj2 = obj1;

Obj1.name = "Nicholas";

alert (obj2.name);//"Nicholas"

4. Pass the parameters:

The parameters of all functions in ECMAScript are passed by value. The delivery of a primitive type value is like a copy of a primitive type variable, whereas a reference to a value of a type is the same as the assignment of a reference type variable. Although access variables are both by value and by reference, parameters can only be 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, or an element in the arguments object). When a value of a reference type is passed 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. For example:

function Addten (num) {

num+=10;

return num;

}

var count = 20;

var result = Addten (count);

alert (count); 20, no change

alert (result); 30

5. Detection type: The type can be detected using the instanceof operator.

The syntax is as follows:

result = Variable instanceof constructor

If the variable is an instance of the given reference type, the instanceof operator returns TRUE. Take a look at the following example:

Alert (person instanceof Object); is the variable person an object?

Alert (colors instanceof Array); is the variable colors an array?

Alert (pattern instanceof REGEXP); is the variable pattern a regexp?

As a rule, all reference types have values that are instances of object.

6. Execution Environment and scope:

(1) Execution Environment: The execution environment is one of the most important concepts in JavaScript. 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. The global execution environment is not destroyed until the application exits. Each function has its own execution environment.

When code executes in an environment, a scope chain of variable objects is created. The purpose of a scope chain is to ensure an orderly access to all variables and functions that the execution environment has access to. Identifier parsing is the process of searching identifiers one level at a scope chain. The search process always starts from the previous segment of the scope chain and goes backward backwards until the identifier is found.

(2) Scope chain:

① Extending the scope chain:

The following statement temporarily adds a variable object to the front end of the scope chain, and the variable object is removed after the code executes.

(a) the catch block of the Try-catch statement;

(b) With statement.

For the WITH statement, the specified object is added to the scope chain. For a catch statement, a new variable object is created that contains the declaration of the thrown error object. Take a look at the following example:

function BuildUrl () {

var qs = "debug = True";

With (location) {

var url = href + qs;

}

return URL;

}

Here, the WITH statement receives the location object, so its variable object contains all the properties and methods of the Location object, and the variable object is added to the front end of the scope chain. A variable QS is defined in the BuildUrl () function. When a variable href is referenced in a with statement, it can be found in the variable object of the current execution environment. When referring to the variable QS, the variable that is defined in BuildUrl () is referenced in the variable object in the function environment. As for the inside of the WITH statement, a variable named URL is defined, so the URL becomes part of the function execution environment, so it can be returned as the value of the function.

② does not have block-level scopes: JavaScript does not have block-level scopes that often cause confusion in understanding.

(a) Declaring variables: variables declared with VAR are automatically added to the nearest environment. Within the function, the closest environment is the local environment of the function, and in the With statement, the closest environment is the function environment. If you initialize a variable without using the var declaration, the variable is automatically added to the global environment.

(b) Query identifiers: When referencing an identifier for reading or writing in an environment, you must search to determine what the identifier actually represents. The search process starts from the front end of the scope and queries the identifier that matches the given name up. If the identifier is found in the local environment, the search process stops and the variable is ready. If the variable name is not found in the local environment, continue searching up the scope chain. The search process goes back to the variable object of the global environment. If the identifier is not found in the global environment, it means that the variable is not declared.

7. Garbage collection

(1) Tag cleanup: The most common garbage collection method in JavaScript is tag cleanup. When the variable enters the environment, it marks the variable as "entering the environment." Logically, it is never possible to release the memory used by variables entering the environment, because they may be consumed as long as the execution flow enters the appropriate environment. When the variable leaves the environment, it is marked as "out of the environment."

(2) Reference count: Another less common garbage collection strategy is called reference counting. The meaning of the reference count is the number of times each value is referenced by the tracking record. When a variable is declared and a reference type value is assigned to the variable, the number of references to that value is 1. If the same value is assigned to another variable, The number of references to this value is added by 1. Conversely, if a variable that contains a reference to this value has another value, the number of references to this value is reduced by 1. When the number of references to this value becomes 0 o'clock, there is no way to access the value again, so you can reclaim the memory space it occupies. That way, when the garbage collector runs again next time, it frees the memory used by those values that have a zero reference count.

(3) Performance issues: Garbage collection is cyclical, and if the amount of memory allocated for a variable is significant, then the amount of recycling is quite large, in which case, determining the time interval for garbage collection is a very important issue.

(4) Managing Memory: The best way to optimize memory consumption is to save only the necessary data for the code in execution. Once the data is no longer useful, it is best to release its reference by setting its value to null-this is called dereferencing. This practice applies to most global variables and properties of global objects. Local variables are automatically dereferenced when they leave the execution environment, as in the following example:

function Createperson (name) {

var Localperson = new Object ();

Localperson. name = name;

return Localperson;

}

var Globalperson = Creatperson ("Nicholas");

Manually de-Globalperson references

Globalperson = null;

However, releasing a reference to a value does not mean that the memory consumed by the value is automatically reclaimed. The real effect of dereferencing is to leave the value out of the execution environment so that the garbage collector can retract it the next time it runs.

JavaScript Advanced Programming: Fourth Chapter

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.