The nature of the loosely typed JavaScript variable determines that it is only a name used to hold a particular value at a particular time. Because there is no provision that defines what data type values A variable must hold, the value of a variable and its data type can be arbitrarily transformed during the life of the script.
Values for basic types and reference types
The ECMAScript variable may contain values of two different data types: the base type value and the reference type value. The base type value refers to those simple data segments (Undefined, number, Boolean, String, Null), whereas reference type values refer to objects that may be composed of multiple values (object);
Unlike other languages, JavaScript does not allow direct access to the location in memory, which means that the object's memory space cannot be manipulated directly. When manipulating an object, it is actually a reference to the object, rather than the actual object, for which the value of the reference type is accessed by reference;
1, the dynamic properties
for values of reference types, we can add properties and methods to them, as well as change and delete their properties and methods, such as:
var person = new Object ();
Person.name = "Lily";
alert (person.name);/result: "Lily"
However, we cannot add attributes to the values of the base type, although doing so does not cause any errors, such as:
var name = "Lily";
Name.age =;
alert (name.age); Result: undefined
2. Copy variable Value
If you copy a value of the base type from one variable to another, the exact same value (including the value, data type) is created on the variable, and the new value is assigned to the new variable, such as:
var num = 5;
var num1 = Num;<span style= "White-space:pre" > </span>//num and NUM1 can participate in any operation but will not affect each other;
When a variable copies a value of a reference type to another variable, the same value that exists in the variable is also copied to the new variable. The difference is that the copy of this value is actually a pointer to an object stored in the heap. After the copy operation is complete, two variables (actually two different references to the same object) will actually refer to the same object. Therefore, changing one of the variables will affect another variable, such as:
var obj1 = new Object ();
var obj2 = obj1;
Obj1.name = "Lily";
alert (obj2.name); "Lily."
3, transfer parameters
When passing a value of a base type to a parameter, the passed value is copied to a local variable (that is, a named parameter, which is an element of the arguments object in the ECMAScript concept). When a value of a reference type is passed to a parameter, the value in memory is copied to a local variable (that is, a reference), so the change of the local variable is reflected outside the function;
function SetName (obj) {
<span style= "White-space:pre" > </span>obj.name = "Lily";
}
var person = new Object ();
SetName (person);
alert (person.name);//"Lily"
function SetName (obj) {
<span style= "White-space:pre" > </span>obj.name = "Lily";
<span style= "White-space:pre" > </span>obj = new Object ();
<span style= "White-space:pre" > </span>obj.name = "Seadog";
}
var person = new Object ();
SetName (person);
alert (person.name);//lily
Even though the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when a function overrides obj (obj = new Object ()), the variable refers to a local object that is automatically destroyed after the function has been executed;
4, detection type
If the variable is an instance of the given reference type, the instanceof operator can be used to detect the reference data type;
Alert (Person instanceof object),//variable person is type object.
alert (colors instanceof arraliany);//variable color is an array type.
alert (pattern instanceof REGEXP);//variable patterns is REGEXP type.
The value of all reference types is an instance of object according to the rule
II. implementation Environment and scope
The execution environment defines other data that a variable or function has access to, determining their respective behavior. Each execution environment has a variable object associated with it, and all variables and functions defined in the environment are its properties and methods;
The global execution environment is the outermost execution environment in which the global execution environment is considered a Window object, so all global variables and functions are created as properties and methods of the Window object , and after all the code in an execution environment has finished executing, The environment is destroyed, and all the variables and function definitions stored therein are destroyed;
When code executes in an environment, a scope chain of variable objects is created. The purpose of a scope chain is to ensure orderly access to all variables and functions that the execution environment has access to. The front end of the scope chain is always the variable object of the environment in which the currently executing code resides. The next variable object in the scope chain comes from the containing environment, while the next variable object comes from the next containing environment, which continues into the global execution environment, and the variable object of the global execution environment is always the last object in the scope chain;
1. Extend Scope chain
2, no block-level scope
Third, garbage collection
1, Mark Clear
2. Reference count
3, Performance problems
4. Manage Memory