This is the ninth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Raw and reference values

ECMAScript variables can contain two different types of data: raw values and reference values. A primitive value is the simplest data, and a Reference value is an object made up of multiple values.

When assigning a value to a variable, the JavaScript engine must determine whether the value is a raw value or a reference value.

The original value

Undefined, Null, Boolean, Number, String, and Symbol. The variable that holds the original value is accessed by value because we operate on the actual value stored in the variable.

Reference value

Reference values are objects that are stored in memory. Unlike other languages, JavaScript does not allow direct access to memory locations, and therefore cannot directly manipulate the memory space where an object resides. When you manipulate an object, you are actually manipulating the reference of the object rather than the actual object itself. To do this, variables with reference values are accessed by reference.

Note that in many languages, strings are represented by objects and are therefore considered reference types. ECMAScript breaks this convention.

Dynamic properties

Raw and reference values are defined in much the same way, by creating a variable and assigning a value to it. However, what you can do with the value after it has been stored in the variable is quite different. For reference values, attributes and methods can be added, modified, and removed at any time. For example, consider the following example:

Let person = new Object() person.name = ' 'console.log(person.name) //"Copy the code

Here, you first create an object and store it in the variable Person. Then, a property named name is added to the object and a string named Ho is assigned to the property. After that, the new property is accessible until the object is destroyed or the property is explicitly deleted.

The original value cannot have attributes, although attempts to add attributes to the original value do not report an error. For example:

Let name = 'he Xiao sheng' name.age = 26 console.log(name.age) // undefinedCopy the code

Here, the code wants to define an age attribute for the string name, and assigns the value 26 to that attribute, and then on the next line, the attribute is gone. Remember, only reference values can dynamically sky-high properties that can be used later.

Note that primitive types can be initialized using only the primitive literal form. If the new keyword is used, JavaScript creates an actual Object of type, but behaves like the original value. Here’s a look at the differences between the two initializations:

Age = 26 name2.age = 27 console.log(name1.age) // undefined console.log(name2.age) // 27 console.log(typeof name1) // string console.log(typeof name2) // objectCopy the code

Duplicate values

In addition to how they are stored, the original and reference values differ when copied through variables. When an original value is assigned to another variable through a variable, the original value is copied to the position of the new variable. Here’s an example:

let num1 = 5
const num2 = num1
Copy the code

Here, num1 contains the value 5. When num2 is initialized to num1, num2 also returns the value 5. This value is completely separate from the 5 stored in NUM1, because it is a copy of that value.

These two variables can be used independently of each other. The process is shown here:

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 is that the copied value here is actually a pointer to an object stored in heap memory. When the operation is complete, the two variables actually point to the same object, so changes on one object are reflected on the other, as shown in the following example:

Let obj1 = new Object() let obj2 = obj1 Obj1. name = 'he_is_console. log(obj2.name) // the' he_is_is_value has been copiedCopy the code

In this example, the variable obj1 holds an instance of a new object. This value is then copied to obj2, where both variables point to the same object. After the property name is created and assigned to obj1, the property is also accessible through obj2 because they both point to the same object.

The figure shows the relationship between variables and objects in heap memory

Public number: xiao He growth, The Buddha department more text, are their own once stepped on the pit or is learned

Interested partners welcome to pay attention to me, I am: He young life. Everybody progress duck together