Bagikan melalui


Copying, Passing, and Comparing Data

The way that JScript copies, passes, and compares data depends on how the data is stored, which in turn depends on the type of the data. JScript stores data either by value or by reference.

By Value vs. By Reference

JScript copies, passes, and compares numbers and Boolean values (true and false) by value. This process allocates a space in computer memory and copies the value of the original into it. Changes to the original do not affect the copy (and vice versa) because the two are separate entities. Two numbers or Boolean values are considered equal if they have the same value.

JScript copies, passes, and compares objects, arrays, and functions by reference. This process essentially creates a reference to the original item and uses the reference as if it were a copy. Changes to the original change both the original and the copy (and vice versa). There is really only one entity; the copy is just another reference to the data.

To successfully compare by reference, the two variables must refer to exactly the same entity. For example, two distinct Array objects will always compare as unequal, even if they contain the same elements. One of the variables must be a reference to the other one for the comparison to succeed. To check if two Arrays hold the same elements, compare the results of the toString() method.

Finally, JScript copies and passes strings by reference. Whether or not the strings are objects determines how the strings are compared. Two String objects (created with new String("something")) are compared by reference. If one (or both) of the strings is a literal or primitive string value, they are compared by value. For more information, see JScript Assignments and Equality.

Note

The ASCII and ANSI character sets are constructed so that capital letters precede lowercase ones in sequence order. For example, "Zoo" compares as less than "aardvark." You can call toUpperCase() or toLowerCase() on both strings if you want to perform a case-insensitive match.

Function Parameters

When JScript passes a parameter to a function by value, it makes a separate copy of that parameter that exists only inside the function. Even though objects and arrays are passed by reference, if a new value in the function directly overwrites them, the new value is not reflected outside the function. Only changes to properties of objects, or elements of arrays, are visible outside the function.

For example, the following program has two functions. The first function overwrites the input parameter, which prevents further the changes to the parameter from affecting the original input argument. The second function changes the property of the object without overwriting the object.

function clobber(param) {
   // Overwrite the parameter; this will not be seen in the calling code
   param = new Object();
   param.message = "This will not work.";
}

function update(param) {
    // Modify the property of the object; this will be seen in the calling code.
    param.message = "I was changed.";
}

// Create an object, and give it a property.
var obj = new Object();
obj.message = "This is the original.";

// Call clobber, and print obj.message.
clobber(obj);
print(obj.message);

// Call update, and print obj.message.
update(obj);
print(obj.message);

The output of this code is:

This is the original.
I was changed.

Data Comparison

JScript can compare data either by value or by reference. To perform a test by value, JScript compares two distinct items to determine whether they are equal to each other. Usually, this comparison is performed on a byte-by-byte basis. When it tests by reference, it checks to see whether two items refer to the same item. If they do, then they compare as equal; if not, although they may contain the exact same values, byte-for-byte, they compare as unequal.

Strings may be compared by value or by reference, depending on whether or not the strings are objects. If both strings are String objects, the strings are compared by reference; otherwise, they are compared by value. This allows two strings to compare as equal if each was created separately from the other but each has the same content. To compare the values of two String objects, first convert the objects to non-object strings with the toString or valueOf methods, and then compare the resulting strings. For more information, see JScript Assignments and Equality.

See Also

Concepts

JScript Assignments and Equality

Data Type Summary

Other Resources

JScript Language Tour

JScript Functions