A deep understanding of the _javascript techniques of passing values and passing references in JavaScript

Source: Internet
Author: User
Tags getdate

1. Pass value (by value)

The value of a variable is copied, which is irrelevant to the original value, which means that even if the new value is modified, the original value will not change, and the base type is passed in JavaScript.

Copy Code code as follows:

function Testpassvalue ()
{
var m=1;
var n=2;
Copies the value of the m,n to the Passvalue
Passvalue (M,n);
Alert (m); will be the original value
}
function Passvalue (a,b)
{
A = A+b; Change the value of a, where a is a copy of the original value
alert (a);
}

Output results:
3

1

2. Reference (by reference).

The reference itself copies a copy to the function, the object to which the reference refers is not replicated (as is the case in Java), and in a function, if the value of the object's property is changed, the value that is accessed through the original reference is the same object as the original reference;

But if you just point a reference to a new object in a function, it will not change the value of the original object, but only the copy of the reference.

Copy Code code as follows:

function Testpassvalue ()
{
var date = new Date (2006,02,27);
Alert (Date.getdate ()); Output is 27
Copy the date reference itself and pass it to passreference, noting that the object that date points to is not replicated
Passreference (date);
Alert (Date.getdate ()); Output is 12
Ditto
Changereference (date);
Alert (Date.getdate ()); Output is also 12
}
function Passreference (DA)
{
Since the DA and the original reference point to the same object, the value of the date property of the object, which is accessed through the original reference, will be the modified value.
Da.setdate (12);
}
function Changereference (DA)
{
When the DA reference is actually a copy of the original reference, the reference itself is assigned a value that will not affect the original reference
Da= new Date (2007,05,11);

Point the DA reference to a new object, at which point the original reference refers to the original object
Alert (Da.getdate ()); Output is 11
}


3 Special String

In JavaScript, string is also referenced. JS only Charat method, and there is no corresponding modification method, and the same as the string in Java, have invariance.

Copy Code code as follows:

var S1 = "Hello";
var s2 = "Hell" + "o";
if (S1 = = s2)
Alert ("S1 = s2"); Will this sentence be enforced? Java-Familiar people may think that it will not execute (this sentence I have a very much criticism, Java will also be executed!) , because the = = in Java is compared with identity. In fact, in JS compared to string== is the value is equal, so this sentence will be executed. But for other object = = comparisons, as in Java, Identity is the same.

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.