This is the 22nd day of my participation in the August Wen Challenge.More challenges in August

Original and reference values

ECMAScript variables can contain two different types of data: raw and reference values. Primitive values are the simplest data (primitive data types, etc.), and reference values are objects made up of multiple values.

A. the original value

  • Variables that hold original values are accessed by value
  • You operate on the actual value stored in the variable

B. reference value

  • Objects stored in memory
  • JavaScriptDirect access to memory locations is not allowed. You cannot directly manipulate the memory space in which objects reside
  • When you manipulate an object, you’re actually manipulating a reference to the object rather than the actual object itself
  • Variables that hold reference values are accessed by reference

1. Dynamic properties

Raw and reference values are defined in a similar way, creating a variable and assigning it a value. For reference values, attributes and methods can be added, modified, and removed at any time. Raw values cannot be attributes, although attempts to add attributes to raw values do not generate an error.

let person = new Object(a); person.name ="Mannqo";
console.log(person.name);  // Mannqo
let name = "Mannqo";
name.age = 18;
console.log(name.age);   // undefined
Copy the code

As you can see, only reference values can dynamically add attributes that can be used later. Note: Primitive types can be initialized using only the primitive literal form. If the new keyword is used, JavaScript creates an instance of type Object.

let nameObj = new String("Mannqo");
nameObj.age = 18;
console.log(nameObj.age);  / / 18
Copy the code

Using the new keyword to create a new object, person, is essentially executing this code;

let person = {};
person.__proto__ = Object.prototype;
Object.call(person);
Copy the code

2. Copy the value

Original value replication: independent of each other, independent use, mutual interference;

Reference value copy: Points to the same object, where the copied value is actually a pointer to an object stored in heap memory. When the operation is complete, both variables actually point to the same object.

So a change in one object is reflected in the other; Consider the following example:

let obj1 = new Object(a);let obj2 = obj1;
obj1.name = 'Mannqo';
console.log(obj2.name);  // Mannqo
Copy the code

This also explains why changing the stereotype of an instance affects other instance objects;

3. Pass parameters

Arguments to all functions in ECMAScript are passed by value. That is, values outside the function are copied to arguments inside the function, just as one variable is copied to another. Variables can be accessed by value and by reference, while passing parameters can only be passed by value.

When a parameter is passed by value, the value is copied to a local variable. If arguments are passed by reference (changes to variables are valid outside the function), the value’s location in memory is stored in a local variable, meaning changes to local variables are reflected outside the function, which is not possible in ECMAScript. This sentence may be a little hard to understand, but consider the following example:

function setName(obj) {
    obj.name = "Mannqo";
    /*obj = new Object(); obj.name = 'yt'; * /    
}
let person = new Object(a); setName(person);console.log(person.name);  // MannqoCopy the code

Pass-by-value, that is, when you enter a function, the object obj points to is the same as the object Person points to. The object obj points to is stored in the heap memory of the global scope, and then modifying the properties of OBj is equivalent to modifying person (obJ can access objects by reference even if they are passed in by value).

Open the two statements commented out above; You can see that the output value has not changed. That’s executionobj = new Object();Change the pointing of obj, it’s no longer pointingpersonThe person object will not be affected if you modify the new object;

When obj is overridden inside a function, it becomes a pointer to a local object that is destroyed at the end of the function.

By reference, it is possible to change the person object to which you originally pointed, which is what happens when you execute the code above. Person is pointing to that person object, so obj is person when you go into the function, and changing the point of obj is changing the point of person;

4. Determine the type

Typeof, while useful for raw values, is less useful for reference values. Because usually we want to know what kind of object it is, not whether it is an object; In this case, we can tell by instanceof;

All reference values are instances of Object, so checking for any reference values and Object constructors through the instanceof operator returns true; Similarly, if you use instanceof to detect a raw value, it will always return false because the original value is not an object;