Primitive types and reference types

type of data

Variables in JavaScript may contain values ​​of two different data types: primitive values ​​and reference values. Primitive values ​​refer to simple pieces of data, including numbers, strings, booleans, null, and undefined; while reference values ​​refer to objects that may consist of multiple values, including arrays, functions, regular expressions, and objects itself.

In JavaScript Language Digest states: "Numbers, strings and booleans 'look like' objects because they have methods, but they are immutable. Objects in JavaScript are mutable keyed collections". The variable mentioned here means that when the value in it is dynamically modified, the original value will also change; while the immutable simple data type class means that even if its value is dynamically modified later, its original value will not change. This is the difference between primitive types and reference types that we will discuss below.

copied value

basic type

If you copy a value of a primitive type from one variable to another, a new value is created on the variable object and then copied to the location assigned to the new variable. The two values ​​are completely independent and can participate in any operate without affecting each other.

  

reference type

When copying a reference type value from one variable to another, the value is also copied to the new allocated space, but the copied value is actually a pointer to an object stored in the heap. After copying, Two variables actually refer to the same object, and changing one of the variables will affect the value of the other, which is the variability mentioned above.

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325662197&siteId=291194637