Home  >  Article  >  Web Front-end  >  In-depth understanding of pass-by-value and pass-by-reference in JavaScript_javascript skills

In-depth understanding of pass-by-value and pass-by-reference in JavaScript_javascript skills

WBOY
WBOYOriginal
2016-05-16 17:10:06844browse

1. Pass value (by value)

The value of the variable is copied and will have nothing to do with the original value. That is to say, even if the new value is modified, the original value will not change. In JavaScript, basic types are all passed by value.

Copy code The code is as follows:

function testPassValue()
{
var m =1;
var n=2;
//Copy the values ​​of m,n and pass them to passValue
passValue(m,n);
alert(m); //Put Is the original value
}
function passValue(a,b)
{
a = a b; //Change the value of a, where a is just a copy of the original value
alert(a);
}

Output result:
3

1

2. By reference.

A copy of the reference itself is passed to the function. The object pointed to by the reference is not copied and passed (the same is true in java). In the function, if the value of the object's attribute is changed, since it points to the same object as the original reference, An object, so the modified value will be accessed through the original reference;

But if you just point the reference to a new object in the function, the value of the original object will not be changed, only the copied reference will be changed.

Copy code The code is as follows:

function testPassValue()
{
var date = new Date(2006,02,27);
alert(date.getDate()); //The output is 27
//Copy the date reference itself and pass it to passReference. Note that the object pointed to by date has not been copied
passReference(date);
alert(date.getDate()); //The output is 12
//Same as above
changeReference(date);
alert(date.getDate()); //The output is still 12
}
function passReference(da)
{
//Since da and the original reference point to the same object, outside the function, what is accessed through the original reference will be the date attribute of the object The value will be the modified value.
da.setDate(12);
}
function changeReference(da)
{
//At this time, the da reference is actually the original reference A copy, reassigning the reference itself, will not affect the original reference
da= new Date(2007,05,11);

//Point the da reference to a new object. At this time, the original reference still points to the original object
alert(da.getDate()); // The output is 11
}


3 Special String

In JavaScript, String is also passed by reference. There is only the charAt method in js, but no corresponding modification method. It is the same as String in java and has immutability.

Copy code The code is as follows:

var s1 = "hello";
var s2 = "hell" "o";
if (s1 == s2)
alert("s1 = s2"); //Will this sentence be executed? People who are familiar with java may think that it will not be executed (I am quite critical of this sentence, and the same is true in java) will be executed! ), because == in Java compares for identity. In fact, String== in js compares whether the values ​​are equal, so this sentence will be executed. But for other Object == comparisons, Identity is the same in java.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn