This is the twelfth day of my participation in the August Wen Challenge.More challenges in August
Original and reference values
ECMAScript variables can contain two pieces of data of different types: the original value and the reference value. Raw values are the simplest data, and reference values are objects made up of multiple values. Undefined, Null, Boolean, Number, String, and Symbol are the six primitive value types. Variables that hold original values are accessed by value, and variables that hold reference values are accessed by reference.
Dynamic properties
For reference values, attributes and methods can be added, modified, and removed at any time.
let juejin = new Object(a); juejin.year =2021;
console.log(juejin.year); / / 2021;
Copy the code
Raw values cannot have attributes, but only reference values can dynamically add attributes that can be used later.
Duplicate values
When assigning an original value from a variable to another variable, the original value is copied to the location of the new variable.
let year1 = 2021;
let year2 = year1;
Copy the code
Here, year1 and year2 both have 2021 values, but are two completely independent variables.
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 here is that the copied value is actually a pointer to an object stored in heap memory. That is, the original value is copied as a value and the reference value is copied as an address.
let juejin = new Object(a);let obj = juejin;
juejin.year = 2021;
console.log(obj.year); / / 2021
Copy the code
Passing parameters
In ECMAScript arguments to functions are passed by value. When passing arguments, the value is copied to a local variable (i.e. a named argument, or a pit in the arguments object), and when passing arguments by reference, the location of the value in memory is saved to a local variable. Obj accesses objects by reference even if they are passed into functions by value. When obj is overridden inside a function, it becomes a pointer to a local object that is destroyed at the end of the function execution.
Determine the type
The typeof operator is used to determine whether a variable is original. You can judge strings, values, Booleans or Booleans or undefined.
Object is returned if the value is null
If you need to further determine what type of object it is, you can use Instanceof
If the original value is checked with instanceof, false is returned