While writing a small paper has not completely dissipated the learning atmosphere, began to focus on consolidating their basic knowledge of JavaScript, for the autumn strokes to do the most basic preparation.
- Variables: JS variables may hold values for two different data types: primitive type values and reference type values.
- The basic types are: Undefined,null,boolean,number and string, and the basic type is access by value;
- The value of a reference type is an object and is referenced by reference;
- When copying variable values, the differences between the two data types are shown in the following:
- After copying the base type values, a copy of this value is created, and two variables of each other can participate in any operation without affecting each other;
- After copying a reference type value, two variables will actually refer to the same object;
- When passed as a parameter, the two data type values are passed as their respective assignment laws, and as far as possible, the parameter can be imagined as a local variable, which can help to understand the concept of "parameter passing by value".
- The typeof operator can be used to detect which basic data type it is, and the instanceof operator can be used to detect which reference data type;
- Scope (execution environment), scope determines the life cycle of a variable:
- The execution environment has a global execution environment and a function execution environment (in different languages, there is no scope for the block of statements);
- Variables declared with Var are automatically added to the closest environment, without using the Var declaration, which is automatically added to the global environment, so it must be declared before the initial variable;
- The garbage collection mechanism works by identifying variables that are no longer in use and releasing the memory they occupy.
- The specific implementation of the browser usually has two policies: Mark Clear, reference count;
- "Mark Clear" is the current mainstream collection algorithm, when accessing non-native JS objects (bom,dom elements) in IE, the "reference counting" algorithm is used, but it causes some problems (when there is a circular reference phenomenon in the code);
- Memory
- The best way to optimize memory is to keep only the necessary data for the code in execution.
- Once the data is no longer useful, it is best to release its reference (that is, the "dereference" method) by setting it to NULL, which applies to the properties of most global variables and global objects;
- The real effect of dereferencing is to leave the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs.
Book Class tolerance: JavaScript Advanced Program Design (P68-82)
JavaScript System Learning Summary-variables, scopes, and memory issues