[From] http://www.imooc.com/article/4585
Basic types and reference types
The variables in JS are not type-sensitive, but actually ECMAScript contains two types, basic types and reference types.
There are 5 basic types: undefined,null,boolean,number,string, the base type is accessed by value, because the actual value saved in the variable can be manipulated.
The value of a reference type is an object that is saved in memory. Unlike other languages, JavaScript does not allow direct access to in-memory locations, which means that the object's memory space cannot be manipulated directly. When manipulating an object, it is actually a reference to the object (also known as a handle) rather than the actual object.
Note: string literals are basic data types, which differ from other languages such as Java.
There is only one type of function parameter transfer in JS: value passing. Take a look at the following example:
functionSetName(obj) { obj name = ' hello ' ;}var p =< Span class= "PLN" > new object (); p. Log (p. Name //hello
The above code creates an object and saves it in the variable p. The variable is then passed to the setName() function and then copied to it obj . Inside this function, the obj p same object is referenced. In other words, even if the variable is passed by value, the obj same object is accessed by reference. Then, when the property is added inside the function obj name , the outside of the function is p also reflected, because p the object pointed to is only one in the heap memory and is a global object. There are many developers who mistakenly assume that objects modified in a local scope are reflected in the global scope, stating that the parameters are passed by reference. To prove that the object is passed by value, let's take a look at the following modified example:
functionSetName(Obj) {Obj.Name= ' Hello ';Obj= new object (); Objname = ' Gray ' ;}var p =< Span class= "PLN" > new object (); p. Log (p. Name //hello
The only difference between this example and the previous example is that two lines of code are added to the SetName () function: One line of code redefined an object for obj, and the other line defines a name property with a different value for the object. After P is passed to SetName (), its Name property is set to "Hello". Then, assign a new object to the variable obj, and set its Name property to "Gray". If P is passed by reference, then P is automatically modified to point to a new object whose Name property has a value of "gray". However, when you next access Person.name, the value displayed is still "Hello". This means that even if the value of the parameter is modified inside the function, the original reference remains unchanged. In fact, when you rewrite obj inside a function, the variable refers to a local object. This local object is destroyed immediately after the function is executed.
You can think of the parameters of the ECMAScript function as local variables.
Type detection
typeofThe operator is useful in detecting the base type and is able to identify the string,number,boolean,undefined, but the pair null and the object do this ‘object‘ . This operator is useless for detecting objects ( Because we usually want to know if a value is not an object, we want to know what it is an instance of.
For the detection object, we have the instanceof operator:
result = variable instanceof constructor
If the base data type is calculated instanceof , the result will be false that the base type is not an object.
Garbage collection
The garbage collection mechanism is actually very simple, periodically identifying variables that are no longer being used and releasing their memory. There are 2 ways to identify a policy for a useless variable:
Mark Purge (Mark-and-sweep)
The garbage collection mechanism used by most browsers.
When a variable enters the environment (for example, when declaring a variable in a function), the variable is marked 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."
You can use any way to tag variables. For example, you can record when a variable enters the environment by flipping a particular bit, or use a "go to environment" variable list and a "Leave environment" variable list to track which variable has changed. In the final analysis, how to tag variables is not important, but the key is to adopt any strategy.
The garbage collector will tag all variables stored in memory at run time (of course, you can use any markup method). It then removes the variables in the environment and the tags of the variables referenced by the variables in the environment. Variables that are tagged later will be considered as variables to be deleted because variables in the environment are inaccessible to those variables. Finally, the garbage collector completes the memory cleanup work, destroys those tagged values, and reclaims the memory space that they occupy.
Reference count (reference counting)
Trace records the number of times each value is referenced. 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 that value is added by 1. Conversely, if a variable that contains a reference to this value has another value, the number of references to the value is reduced by 1. When the number of references to this value becomes 0 o'clock, it means that there is no way to access the value again, so that it 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.
There is a serious problem with circular referencing in this way. A circular reference refers to a pointer to object B that is contained in object A, and object B contains a reference to object A.
functionProblem() { var Objecta = new object (); var OBJECTB = new object (); Objectasomeotherobject = Objectb;. Anotherobject = Objecta; /span>
In this example, Objecta and OBJECTB are referred to each other by their respective properties, that is, both objects have a reference count of 2. In implementations that take a markup cleanup policy, these two objects leave the scope after the function executes, so this mutual reference is not an issue. However, in implementations with reference counting policies, objecta and OBJECTB will continue to exist when the functions have been executed, because their references will never be 0. If this function is repeated multiple calls, it will result in a large amount of memory not being recycled. To do this, Netscape discarded the reference counting method in Navigator 4.0, instead of using tag cleanup to implement its garbage collection mechanism. However, the trouble with reference counting does not end there.
Some of the objects in IE are not native JavaScript objects. For example, the objects in their BOM and Dom are implemented using C + + as COM (Component object model, Component object models) objects, and the garbage collection mechanism of COM objects is a reference counting strategy. Therefore, even though the JavaScript engine of IE is implemented using the tag purge policy, the COM objects that JavaScript accesses are still based on the reference counting policy. In other words, whenever a COM object is involved in IE, there is a circular reference problem. To solve these problems, IE9 has converted both BOM and DOM objects into real JavaScript objects. This avoids the problems caused by the coexistence of two garbage collection algorithms, and eliminates the common memory leaks.
[Go] variables and garbage collection in JavaScript