Javscript primitive and reference types

“The loosely typed nature of JavaScript variables” dictates that “a variable is just a name that holds a particular value at a particular time.”

Since there are no rules defining what data type values a variable must hold, the value of a variable and its data type can change over the life of the script.

Variables may contain values of two different data types:

  1. Basic types of
  2. Simple data segments that access simple data segments stored in the stack by value, that is, their values are stored directly “where variables are accessed.”
  3. Reference types
  4. An object stored in the heap, that is, “the value stored in a variable is a pointer to the memory address where the object is stored.”
  5. An object that may consist of multiple values
  6. The value of a reference type is an object held in memory
  7. Js does not allow direct access to locations in memory, that is, the memory space in which objects cannot be manipulated directly
  8. Values of reference types are real columns of Object

Attributes can only be added and removed dynamically to reference type values

1. Differences

  1. Different memory allocation when declaring variables
    1. “Base Type” is stored in “stack memory”
    2. “Reference Type” is stored in “heap memory”
  2. Different memory allocation mechanisms lead to different access mechanisms
    1. Javascript does not allow you to access objects stored in the heap directly, so when you access an object, you first get the address of the object in the heap, and then use that address to get the value of the object. This is known as “access by reference”.
    2. Primitive values are directly accessible.
  3. The difference between copying a variable
    1. Original value: When a variable containing the original value is copied to another variable, a copy of the original value is assigned to the new variable. “The two variables are then completely independent, they just have the same value.”
    2. Reference value: When a variable that holds the memory address of an object is copied to another variable, the memory address is assigned to the new variable. That is, both variables refer to the same object in heap memory, and changes made by either of them are reflected in the other.
  4. Differences in parameter passing (*The process of copying arguments to parameters *)
    1. First we should make it clear that arguments to all ECMAScript functions are “passed by value”.
    2. But why is there still a difference when it comes to values of primitive and reference types? Just because of the difference in memory allocation
      1. Raw value: just pass the value of the variable to the parameter, and the parameter and the variable do not affect each other.
      2. Reference value: The value in the object variable is the memory address of the object in the heap. Keep this in mind! So the value that it passes is the memory address, which is why changes to this parameter inside the function are external, because they all point to the same object.

2020_07_21_dN5SjH
2020_07_21_oXd831

2. Detect data types

Is a variable a primitive data type? Using typeof, you can determine whether a variable is a string, a value, a Boolean, or undefined.

If the variable is an object or Null, Typeof displays as object

What if we know that a variable is an object, but what kind of object are we checking? Use the instanceof

Variable instanceof constructor

var arr = [1.2.3]
var obj = {name:'zs'}
arr instanceof Array   // true
obj instanceof Object  // true
Copy the code

This article is formatted using MDNICE

The article lists

Basic Concepts of JavaScript

JavaScript data types

JavaScript primitive types and reference types

Javascript execution environment and scope

Javascript garbage collection mechanism

Javascript array