Javascript Assignment Mechanism Details _ basics

Source: Internet
Author: User

Today I answered a question about Javascript, which involves assigning problems, so I want to make a good summary of this problem.

Copy Code code as follows:

var a = ' test ';
var B = function () {};
B.A = ' Test ';
function Change (m, N) {
m = ' change ';
N.A = ' Change ';
}
Change (A, b);

Does the value of variables A and B change when you execute the above code?

Raw values and reference values

In the previous article, the original value and reference value, the original value refers to the Undefined, Null, Boolean, number, String, and so on, they are stored in the stack, and the reference value is integrated from Object, it is stored in the heap.
Here's a clear distinction between the two:

Copy Code code as follows:

var a = ' test ';
var B = new String (' Test ');
var A = ' true ';
var B = new Boolean (' true ');

The above four variables, a and a are the original values, while B and B are reference values.

Assignment mechanism

With a clear distinction between the original value and the reference value, you can specifically describe the Javascript assignment mechanism:

In Javascript, for variables of the original value type, each assignment generates a copy, and for a reference value, as its name, it is assigned by reference, pointing to the memory of the same storage object.
Assignment of the original value:

Copy Code code as follows:

var a = 1;//original value
var B = a;//generates a copy to variable B
B = 2;//has nothing to do with a
alert (a);//Output 1

Assignment of reference values:

Copy Code code as follows:

var a= new Object ()//reference value
a.x = 1;
var B = a;//Reference assignment, pointing to the same memory place
b.x = 2;//modification B will affect A
alert (a.x);//Output 2

Parameter passing

Now let's look at how the two types of values are passed to the function parameter.
1. Pass the original value

Copy Code code as follows:

var a = 1;
function test (m) {
m = 2;
}
Test (a);
alert (a);//Output 1

The output is 1, so we know that the function simply passes the value of the variable in, so the value of M in the function body is 1, and then it is assigned 2, which does not affect the external variable A.

2. Passing Reference values

Copy Code code as follows:

var a= new Object ();
a.x = 1
function test (M) {
m.x = 2;
}
Test (A);
alert (a.x);//Output 2

The output is 2, so we know that the function passes the address of the variable in, so the M in the function body gets the address that is passed, so a property x assigned to 2 also affects a that points to the same memory address.

Summarize

Now come back to the question that begins:

Copy Code code as follows:

var a = ' test ';
var B = function () {};
B.A = ' Test ';
function Change (m, N) {
m = ' change ';
N.A = ' Change ';
}
Change (A, b);

Variable A is the original value, the variable B is the reference value, passed into the function body is a value, one is the address, so after the function runs, the variable a will not change, and the value of the variable B will change.

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.