A variable in JavaScript is just a name that holds a particular value at a particular time

Values for base and reference types

There are two types of values in ECMAScript:

  • The value of the base data type
    • Stored in stack memory, the values themselves can be accessed and manipulated directly
  • A value that refers to a data type
    • In ECMAScript, there is only one reference type, which is object
    • The value of a reference type contains three concepts: a reference to an object, an object, and its memory space
    • When we declare a variable and assign an object to the variable, we are actually assigning a reference to the object to the variable and storing it in stack memory. The value of the object is stored in heap memory that is not directly accessible to the developer

Dynamic properties

  • For a value of a reference type, we can add attributes and methods to it, change or delete its attributes and methods
  • You cannot add attributes and methods to values of primitive types

Copy the value of a variable

  • For values of primitive types, the concrete values are copied
  • For a value of a reference type, the address of the value in heap memory is copied. If the value of the reference type is changed at this time, the value of both the copied and copied variables will change

Passing parameters

All function arguments in ECMAScript are passed by value, meaning that passing a value outside a function to a parameter inside the function is the same as copying a value from one variable to another

  • The argument inside a function is a local variable whose scope is the function itself
  • If the value is of a primitive type, simply copy the value itself
  • If the value is of a reference type, the reference to the value is passed to the parameter, and if the value is changed inside the function, the value outside the function is changed accordingly
    function setName(obj){
        obj.name = "oswald"
    }
    var person = {};
    setName(person);
    alert(person.name);         // "oswald"
Copy the code

Detection of type

The typeof operator is used to detect values of primitive types, and the instanceof operator is used to detect values of reference types

If the variable is an instanceof a given reference type, then the instanceof operator returns true, as judged by the stereotype chain

    alert(person instanceof Object);        // Person is a value of type Object
    alert(person instanceof Array);         Is person a value of type Array
    alert(person instanceof RegExp);        Is person a value of type RegExp (re)
Copy the code

Execution environment and scope

The execution environment is one of the most important concepts in JavaScript

  • The execution environment defines whether a variable or function has access to other data and determines their respective behavior. Each execution environment has a variable object associated with it. All variables and functions defined in the environment are stored in this object and cannot be accessed by the code we write. But the parser uses it behind the scenes as it processes the data
  • The global execution environment is the most peripheral execution environment, and in Web browsers, the global execution environment is referred to as the Window object
  • Each function has its own execution environment
  • When code executes in an environment, it creates a scope chain of variable objects. The purpose of the scope chain is to ensure orderly access to variables and functions that the execution environment has access to. The top of the scope chain is always the variable object of the current environment
  • If the execution environment is a function, its active object is treated as a variable object. The active object starts out with only one variable, the Arguments object that contains the function arguments, which only exists inside the function
  • The next variable object in the scope chain comes from the external environment one level above the current environment and up until the global execution environment is found. The variable object in the global execution environment is always the last object in the scope chain
  • Identifier resolution is the process of searching identifiers level by level along the scope chain until the global execution environment is found

There is no block-level scope

There is no block-level scope in ES5. All variables declared using VAR are automatically added to the nearest environment, variables declared inside a function are added to the execution environment of the function, and variables not declared using VAR are added to the global environment

Query identifier

  • Query identifier will be the most front end, from the scope chain is the current execution environment, search with a given name matching identifier, if not the current environment, runs up the scope chain is looking for, the global environment has been found, if found in the process, to stop search, if could not find in the global environment, Indicates that the variable is not declared
  • If an identifier with the same name exists in different environments, only the first identifier found in the parent environment is used

Garbage collection

JavaScript has automatic garbage collection, and the execution environment is responsible for managing the memory used during code execution

Mark clear

  • The most common form of garbage collection in JavaScript is tag cleanup, marking a variable as “in” when it enters the environment and “out” when it leaves the environment
  • The garbage collector marks all variables stored in memory, removes the marks of variables in the environment and those referenced by variables in the environment, and then the remaining marked variables are treated as variables to be deleted because the variables in the environment can no longer access them, Finally, the garbage collector destroys the tagged values and reclaims the memory they occupy

Memory management

  • The main reason we manage memory in JavaScript is to prevent a memory leak that causes a web page running JavaScript to take up all of the system’s memory and crash the system
  • Method to optimize the memory footprint is preserve only the necessary data for code, once the data is no longer needed, through the best value is set to null to remove references, this practice is suitable for global variables, global object attributes, and circular reference variable references, because local variables will be automatically remove references in leaving the execution environment
  • The purpose of dereferencing is to take the value out of the execution environment so that it can be reclaimed the next time the garbage collector runs