“This is the sixth day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
Original and reference values
Primitive values: simplest data (Undefined, Null, Boolean, Number, String, Symbol). We’re dealing with the actual value stored in the variable
Reference value: An object composed of multiple values. Is an object stored in memory. Js does not allow direct access to memory locations and cannot directly manipulate the memory space where objects reside. The actual operation is a reference to the object.
Dynamic properties
Both the original and reference values are defined by creating a variable and then assigning a value to it.
- Reference values can be added, deleted, or modified at any time with their properties and methods.
let obj1 = new Object(a); obj1.name ="Nike"
console.log(obj1.name) // Nike
Copy the code
The property added to object obj1 can be accessed until the object is destroyed or the property is explicitly deleted.
- Raw values cannot have attributes
let oldValue = "old"
oldValue.name = "mm"
console.log(oldValue.name) // undefined
Copy the code
Raw values can only be in raw literal form. If you use the new keyword, javaScript creates an instance of type Object that behaves like the original value
let obj = new String("font")
obj.name = "objName"
console.log(obj.name) // objName
console.log(typeof obj) // object
Copy the code
Duplicate values
First compare the replication that copies the original value to the reference value
/ / the original value
let value1 = 5;
let value2 = value1; // Copy the value of value1 to value2
// We modify the value of value1 and print value2
value1 = 8;
console.log(value2); / / or 5
Copy the code
/ / reference value
let obj1 = new Object(a);let obj2 = obj1; // copy obj1 to obj2
// Change obj1 and print obj2
obj1.name = "test"
console.log(obj2) // {name: 'test'}
Copy the code
The difference between
- The copying of the original values is non-interfering.
- A copy of a reference value is actually a pointer to the same object. Change one and the other will change
Passing parameters
Arguments to ECMAScript functions are passed by value, meaning that values outside the function are copied to arguments inside the function.
The parameter may take the form of a raw value or a reference value.
/ / the original value
function Add(num){ Let num = count
num += 10;
return num;
}
let count = 20; / / variable count
let result = Add(count) // Execute the function to pass the parameter count
consol.log(count); // there is no change in 20
console.log(result); / / 30
Copy the code
/ / reference value
function Change(obj){
obj.name = "test"
return obj
}
let person = new Object(a)// instantiate the object
let pObj = Change(person) // Execute the function as a person object
console.log(person) // {name:'test'}
console.log(pObj) // {name:'test'}
Copy the code
We said that arguments are passed by value, but in the reference value example above, we add the name attribute to obj inside the function. The external object of the function also reflects this change, and we feel that the parameters of the reference type are passed by value. So let’s look at the following example
function Change(obj){
obj.name = "test"
obj = new Object(a);// Change: add two lines of code
obj.name = "wgh"
return obj
}
let person = new Object(a)// instantiate the object
let pObj = Change(person) // Execute the function as a person object
console.log(person) // {name:'test'}
console.log(pObj) // {name:'wgh'}
Copy the code
-
If person is passed by reference, then person should automatically point to an object whose name is WGH. But the result of our revisit is test, indicating that the original reference value is still the same.
-
Obj overrides things inside the function to become a pointer to a local object. This local object is destroyed at the end of the function execution.
Arguments to ECMAScript functions are local variables
Determine the type
Typeof applies to primitive types, and only object types can be identified for reference values. But we usually want to know what kind of object it is. (for example, typeof NULL returns type Object)
Hence the instanceof operator
let person = [{'id':1}]
console.log(person instanceof Object) // true
console.log(person instanceof Array) // false
console.log(person instanceof RegExp) // false
Copy the code
Checking the original value with instanceof always returns false
📢 Typeof also returns function when checking functions