[JS] Assigning a person to a faint javascript variable

Source: Internet
Author: User

Assigning values to variables

This article is reproduced from http://hellobug.github.io/blog/javascript-variable-assignment/

Let's start with a few examples to make sure the starting point is Halo State ~:P

Example 1.1

1234  
var a = "apple";var b = a;a = "banana";b

Supposedly, B = A After, A is what value B should be followed by what value ~
However, the b result is a "apple" value at the beginning of the assignment.

Example 1.2

1234     
 var a = Span class= "p" >{name:  "apple" }; var b = a; a. Name =  "banana" ; b. Name                 

This time, B and then a change, the b.name result is"banana"

Example 1.3

12
  34     
 var a = {name:  "apple" }; var b = a;< Span class= "line" >a = {name: span class= "s2" > "banana" }; b. Name                 

This time B insisted on himself, the b.name result is"apple"

Example 1.4

1234     
 var a = Span class= "p" >{count: 2}; var b = a. Count; a. Count = 3; b               

The same b result is the beginning 2 , b in the end is to make what?!

====================== begins to explain the split line ==============================

In fact, B is very innocent, this is from the ECMAScript variable value type to say ~

There are two kinds of types:

    • basic type (primitive values) -includes undefined, Null, Boolean, number, and string five basic data types
    • reference types (reference values) -objects that are stored in memory cannot be manipulated directly, but can only be manipulated by address references stored in variables

Now take a look at the first example例1.1

1
var a = "apple";

"apple"is a string type, which is the base type, at which point the value is stored:

1
var b = a;

A copy of the value of a is then assigned to B:

So, from A and b squarely, how to modify each other will not affect the other ~

Let's look at a second example.例1.2

1
var a = {name: "apple"};

{name: "apple"}is an object that belongs to the reference type, and the value before and after the assignment is stored like this:

So when it's a.name = "banana"; time to change the property value of the object in memory that everyone points to, the b.name value is changed.

例1.3In

Example 1.3

123456    
 var a = {name:  "apple" }; var b = a;< Span class= "line" >a = {name: span class= "s2" > "banana" }; //{name: "Banana"} is a new object in memory, // The address of the A variable is also pointed to the new object, so it's irrelevant to B. b.//or "apple"             

例1.4In

Example 1.4

123456    
 var a = {count: 2};< Span class= "line" >var b = a.< Span class= "NX" >count; //A.count is the number type, so the value is copied to B,//from then on how to modify the B has nothing to do with a. Count = 3; b //or 2             

Small summary, variable assignment always copy a copy, if it is the basic type, copy is the actual value, if it is a reference type, copy is the address value pointing to object, so point to the same object.

Variable comparison

Take a closer look at the comparison of variables ~

Example 2.1

123 
var a = "apple";var b = "apple";a == b

This is no problem, the result must be true .

What about this one?

Example 2.2

123 
var a = ["apple"];var b = ["apple"];a == b

Although the two numbers are identical, the result is still false .

In fact, the principle is the same, for the basic type, compared to the actual value, and for the reference type (array is an object), compared to the address value , although the two array content is the same, but they are in memory is two object, the address is not the same, So the result of the comparison is false .

Posted by Hellobug Jan 27th, javascript

[JS] Assigning a person to a faint javascript variable

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.