Home > front end >  Js problem
Js problem

Time:09-19

 
Var obj1={
Name: "true",
}
var obj2=obj1;
Obj1. Name="false";
The console. The log (obj2. Name); //print false

Var obj1={
Name: "true",
}
var obj2=obj1;
Obj1={
Name: "false",
}
The console. The log (obj2. Name); //print true

Want to know why the results of the two kinds of printing is not the same?

CodePudding user response:

The first case, the two variables point to the same memory address,

CodePudding user response:

reference 1st floor Xo_QX response:
the first case, the two variables point to the same memory address,

I know this place, because it is a reference value, just the same, I just don't understand the second

CodePudding user response:

Variable assignment as an object, variable to hold the object's memory addresses, so the second obj1 address is changed, and obj2 is not the same object,

CodePudding user response:

Js objects and arrays are passed by reference address,
Reference is different from the pointer in C language, reference only effective when reading data, when the write data of variables, the original reference will be disconnected,

When an object from one variable to another variable, two variables point to the same array reference address,
var obj2=obj1;
Obj2 and obj1 variables point to the same object reference address,

But this reference variables are read only available at the time, when the variable assignment again, the original reference will be disconnected,

Obj1={name: "false",}
Obj1 variable to point to a new object reference address, will not affect the obj2 variables,

CodePudding user response:

references 4 floor sky waves reply:
in js objects and arrays are passed by reference address,
Reference is different from the pointer in C language, reference only effective when reading data, when the write data of variables, the original reference will be disconnected,

When an object from one variable to another variable, two variables point to the same array reference address,
var obj2=obj1;
Obj2 and obj1 variables point to the same object reference address,

But this reference variables are read only available at the time, when the variable assignment again, the original reference will be disconnected,

Obj1={name: "false",}
Obj1 variable to point to a new object reference address, will not affect the obj2 variables,

Is so ah, thank you

CodePudding user response:

reference Xo_QX reply: 3/f
to variable assignment as an object, variable to hold the object's memory addresses, so the second obj1 address is changed, and obj2 is not the same object,

Thank you for your reply!
  • Related