JavaScript Variables : Scope, and Memory Issues

The values of basic types and reference types

Two types of data for values

  • Basic types are saved on the stack
  • Save reference types in the heap

5 basic types

  • Undefined
  • Null
  • Boolean
  • Number
  • String

For the value of a reference type, we can add, change, and delete its properties and methods.

If a basic type value is copied from one variable to another, a new value is created on the variable object and then copied to the position assigned to the new variable.

When copying a value of a reference type from one variable to another, the value is actually a pointer, and after the copy operation ends, both variables will reference the same object.

All parameters of functions in JavaScript are passed by value. When passing basic data types, the value is copied to a local variable. When passing reference types, the memory address of this value is copied to a local variable.

Typedef is the best tool to determine whether a variable is a string, numerical value, Boolean value, or undefined. If the value of the variable is an object or null, it will return object

When we want to know what type of object a value is, we use the instance of operator.

Execution environment and scope

Execution environment abbreviated as environment

The environment defines other data that variables or functions have access to, and each environment has an object variable associated with it. All variables and functions defined in the environment are stored in this object.

The global environment is the outermost environment and is considered a window object.

After all the code in a certain environment is executed, the environment is destroyed, and all variables and function definitions saved in it are also destroyed.

Each function has its own environment

When code is executed in an environment, a scope chain is created, and the beginning of the scope is the object variable of the environment where the current code is located.

If the environment is a function, then its object is arguments, and the next object in the scope chain comes from the object variable of the environment that contains this function, with the chain continuously extending outward until the global environment.

Search for identifiers from the front end of the chain until the end. So the internal environment can access the external environment through the scope chain, but the external environment cannot access any variables and functions in the internal environment.

The with and catch blocks can add an object variable at the front end of the scope, with will add the specified object, and catch will add the declaration of the thrown error object.

There is no block level scope in JavaScript, such as if and for loops where parentheses do not have their own scope, so variables defined within them still exist after the parentheses end.

Variables declared with var will be added to the closest environment, while variables declared without var will be automatically added to the global environment. We recommend declaring variables before initializing them.

During the search process, once found, the search will be stopped. So if the child and parent have the same identifier, the search will only stop when the child is found.

Garbage Collection

JS has an automatic garbage collection mechanism, and the garbage collector will execute this operation periodically at fixed time intervals.

Two strategies for mark clearing and reference counting

  • Mark removal mostly involves marking variables as entering the environment and leaving the environment when they leave. The garbage collector will destroy those marked values and reclaim the memory space they occupy.
  • The reference count is similar to OC, so I won’t write it if I converted it from iOS.

The best way to optimize memory is to set the data to null once it is no longer useful, which applies to global variables and properties.