First, things begin with a question
<script>
function A () {
this.name= "";
this.age=18;
}
var a1=new A ();
A1.name= "the Nile cock";
var a2=a1;
A2.name= "Blue";
Console.log (A1.name); It's blue.
var str1= "Nicholas Cock";
var str2=str1;
Str2= "Blue";
Console.log (STR1); No change, or the Nicholas
a2=a1 </script> simple
;
a2.name=xxx; A1.name will become
str2=str1;
str2=xxx; STR1 won't change.
Welcome to think nonsense too many students go straight to the end to see "technical flow explanation" ha, then we start
Apply a more Vulgar words: "I know the truth, but why the string will not change it", JavaScript as a high-level language does not have the concept of pointers, but it is always plagued by this thing, today I want to try to let people understand this concept, if you want to understand this thing, Please come to understand a very simple thing first:
Suppose you have a girlfriend called Dream Qi, like to wear a yellow hat, you helped her to buy a red hat, ask: Dream Qi with what color hat?
Answer: Red
Suppose you have a girlfriend called Dream Qi, like wearing a yellow hat, you directly changed a love with a red Hat girlfriend, ask: Dream Qi with what color hat?
Answer: Still yellow, not changed
If we understand the question of the girl who has the heart of a carrot and a hat, then the problem becomes very simple, and the code says again:
Situation 1: Change the Hat
you. Girlfriend = Dream Qi;
You, girlfriend. Hat = red;
Console.log (dream kiki hat); Red
//Situation 2: Change
your girlfriend. Girlfriend = Dream Qi;
you. Girlfriend = Ann Yi;
Console.log (dream kiki hat); Yellow, no change.
To put it simply, you've changed your girlfriend's hat, right?
To get a complete understanding of the problem step 1/2:
Let's make this messy relationship a little more messy. [Cover your Face]
Suppose John and Dick, all find the same girlfriend, John bought her a hat, ask: Dick girlfriend change hat?
A: Change
Suppose John and Dick, all find the same girlfriend, John can't carry, and new find a girlfriend, ask: this with Dick girlfriend hat is related?
A: It's a hairy relationship.
The above chaotic relationship, expressed in a program, is:
Situation 1. Change
the hat john girlfriend = Dick girlfriend; The legendary quote: the first time to see the infidelity can be said so fresh and refined
john Girlfriend. Hat = green hat;
Console.log (Dick girlfriend. hat); Changed, green
/situation 2. Have a hairy relationship with a hat
john girlfriend = Dick girlfriend;
John girlfriend = new girlfriend;
Console.log (Dick girlfriend. hat); I don't know what color, but it hasn't changed.
To get a complete understanding of the problem step 2/2:
Go directly to the code, explain the comments.
Situation 1: Hat
var john girlfriend =new Girlfriend ();//Too convenient there is no
var dick girlfriend = John girlfriend;//quote
john Girlfriend. Hat = red;
Console.log (Dick girlfriend. hat); Red, changed
//situation 2: Have Mao relationship with hat
var john girlfriend =new Girlfriend ();
var dick girlfriend = John girlfriend;
John girlfriend = another;
Console.log (Dick girlfriend. hat); It's not changing, it's hairy with hats.
So now please follow me and look over the questions above
Situation 1
var a1=new A ();
var a2=a1;
Note: At this time, A1 and A2 are the same thing, quote, you know
a2.name= "blue";//You bought a new hat for your girlfriend
Console.log (a1.name);//her hat is blue.
//Situation 2
var str1= "AAA";
var str2=str1;
Note: At this time, str1 and str2 are also the same thing, also refers to the
str2= "blue";//You changed a girlfriend
Console.log (str1);//Your original girlfriend didn't change.
When you are old, you will be very long-winded, directly above the picture how good:
Situation 1:a2.name= "Blue"
Situation 2:str2= "Blue"
========================== Pure Technology Flow divider line ==========================
1. Pointer: Represents a variable or attribute to WHO
2. Assignment: Represents a change in the direction of a variable or property
Combined with:
A1=A2;
A2.name= ' Blue ';
The change a2.name points to ' Blue ', but A1 and A2 still point to the same object, so:a1.name=> ' blue '
str1=str2;
Str2= ' Blue ';
Change str2 points to ' blue ' when str1 and str2 no longer point to the same object, so: str1 unchanged
The above is a small set of JavaScript to introduce the questions (pointers, hats and girlfriends), I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!