4.1.2 Copy Value When assigning an original value from a variable to another variable, the original value is copied to the position of the new variable. The two variables are used independently and do not affect each other. When a reference value is assigned from one variable to another, the value stored in the variable is copied to the location of the new variable. The difference here is that the copied value is actually a pointer to an object stored in heap memory. After the operation is complete, both variables actually refer to the same object, so changes on one object are reflected on the other.
The best way to determine whether a variable is a string, value, Boolean or undervalue is typeof. If the object is NULL, then Typeof returns object
Typeof is of little use for reference values, and the instanceof operator returns true if the variable is an instanceof a given reference type. person instanceof Object person instanceof Array person instanceof RegExp
4.2 Execution Context and the context of scoped variables or functions determines what data they can access and how they behave. Each context has an associated variable object on which all variables and functions defined in this context reside and cannot be accessed by code.
The global context is the outermost context, and in the browser, the global context is what we call the Window object. When the code in the context executes, it creates a chain of scopes for the variable object. This chain of scopes determines the order in which the code at each level of context accesses variables and functions. The variable object of the context in which the code is executing is always at the front of the scope chain.
4.3 Garbage Collection Js is a language that uses garbage collection. The execution environment is responsible for managing memory during code execution. The idea is simple: the garbage collector runs automatically every once in a while.
The most common garbage collection strategy in JS is tag cleanup. When a variable is entered into a context, such as declaring a variable inside a function, the variable is marked as existing in the context. When the garbage collector runs, it marks all variables stored in memory. It then strips out all variables in the context, and all variables referenced by variables in the context. Variables tagged after this point are deleted because they are not accessible to any variables in context. The garbage collector then does a memory cleanup, destroying all tagged values and reclaiming their memory.
4.3.4 Memory Management Keeping the memory footprint to a small value leads to better page performance. The best way to optimize memory footprint is to ensure that only necessary data is saved when executing code. If the data is no longer necessary, set it to NULL, freeing its reference. This is also called dereferencing. This recommendation works best for properties of global objects and global variables. Local variables are automatically dereferenced when they go out of scope.
1. Improve performance with const and let declarations 2. Hide classes and delete operation 3. Memory leak 4. Static allocation with object pooling