1. Basic type and reference type
(1) The basic types in JavaScript are value types, including string and value types.
(2) values (objects) of the dynamic attribute reference type can be dynamically added, modified, or deleted, but the value of the basic type is not. Although no error is reported.
// Dynamically Add the property var person = new Object (); person. name = "Nicolas"; // dynamically Add the property name alert (person. name); // Nicolas
(3) copy the value of a variable. If the value of variable1 is assigned to another variable variable2, if both are basic types, the change of one variable value will not affect the other. For the reference type, the value assignment between variables (assigning a variable name to another variable name through the value assignment operator) is actually a copy of the address reference (pointer) pointing to the same object. Therefore, although the two variables have different names, they all point to the same object pointer. The values of the objects obtained from these two variables are always equal, changing the value of an object through a variable will also be reflected to another variable. If the variables variable1 and variable2 point to the same object, the value assigned to the variable variable1 is not the name of another variable, but the value of the object, you can directly change the value of the object pointed to by the variable variable1. At the same time, the value of the object pointed to by the variable variable2 will also change.
(4) parameter transfer all function parameters are passed by value, that is, the external value of the function is copied to the internal parameter of the function. If a value of the basic type is passed to the parameter, the passed value will be copied to a local variable (that is, a named parameter, or a form parameter, or an element in the arguments object ), changes to this local variable will not be reflected outside the function. If a reference type value is passed to the parameter, the address in the memory will be copied to a local variable, changes to this local variable are reflected outside the function.
// ----------------------------------------------------- // Example of a real parameter as a value type: function addTen (num) {num + = 10; return num ;}
Var count = 20; var result = addTen (count); alert (count); // 20-no change alert (result); // 30
// ------------------------------------------------------ // Example where the real parameter is of the reference type: function setName (obj) {obj. name = "Nicholas ";}
Var person = new Object (); setName (person); alert (person. name); // Nicolas
// ------------------------------------------------------- // Function setName (obj) {obj. name = "Nicolas"; obj = new Object (); obj. name = "Greg ";}
Var person = new Object (); setName (person); // if it is passed by reference, the result of the following statement will be Greg // but the result is Nicholas, so it turns out that alert (person. name); // Nicolas
(5) You can use the instanceof operator to determine the specific type of the object to be referenced. The main purpose of this operator is to determine the type of the custom object. The result is a Boolean value. It is used as follows:
Result = variable instanceof constructor
Or
Result = object instanceof class
2. Execution Environment and scope
(1) There is no block-level scope in the local code block in JavaScript (Internal Flow Control statements of functions, excluding function and object code blocks) variables declared in can be accessed outside the code block (only functions or objects that directly contain the code block ), in programming languages such as C, C ++, C #, and Java, internal block variables cannot be accessed externally. In JavaScript, the previous value of this variable cannot be eliminated even if a variable is declared again. For example:
Function outputNumbers (count) {for (var I = 0; I <count; I ++) {alert (I );}
Var I; // variable redeclared alert (I); // count}
However, if the post-declared I is initialized, the I value will be the post-declared value.
3. garbage collection, performance, and memory management
(1) Avoid circular references. The following code will generate circular references. In IE8 and earlier versions, you will not be able to clear variables that are no longer in use.
Var element = document. getElementById ("some_element"); var myObject = new Object (); myObject. element = element; element. someObject = myObject;
(2) For a global reference type variable, the attributes of the global reference variable and the variable that occurs in the circular application should be assigned null if it is no longer used, in this way, the next time the Garbage Collector runs, it can be cleared from the memory.