In JScript, data processing depends on the data type.
Compare by value and by reference
Values of the numbers and Boolean types (true and false) are copied, transmitted, and compared by value. When copying or passing by value, a space will be allocated in the computer memory and the original value will be copied to it. Then, even if you change the original value
These two values affect the copied values (the same way in turn) because they are independent entities.
Objects, arrays, and functions are copied, transferred, and compared by reference. When copying or passing by address, you actually create a pointer to the original item and use the pointer just like copying. If the original item is subsequently changed, the original item is also changed
Item and copy item (the same way ). In fact, there is only one entity. A "copy" is not a real copy, but a reference to the data.
When comparing by reference, to make the comparison successful, the two variables must refer to the exact same entity. For example, two different array objects are not equal even if they contain the same elements. To be successful, one of the variables must be another reference.
. To check whether two arrays contain the same elements, compare the results of the tostring () method.
Finally, strings are copied and transmitted by reference, but compared by value. Note that if there are two string objects (created using new string ("something"), compare them by reference. However, if either or both of them are
String values, compare them by value.
Note that given the ASCII and ANSI character set constructor, uppercase letters are placed before lowercase letters in sequence. For example, "Zoo" is smaller than "aardvark ". To perform case-insensitive matching, you can call
Touppercase () or tolowercase ().
PASS Parameters to Functions
Passing a parameter to a function by value is an independent copy of the parameter, that is, a duplicate that only exists in the function. Even if you pass objects and arrays by reference, if you directly overwrite the original value with a new value in the function, the new value is not reflected outside the function. Only
It can be seen outside the function only when the attributes of the object or the elements of the array change.
For example (using the IE Object Mode ):
// BookCodeSegment corruption (overwrite) its parameters, so
// The Call Code does not reflect any changes.
Function clobber (PARAM)
{
// Destroy the parameter. In the call code
// You cannot see it.
Param = new object ();
Param. Message = "This will not work ";
}
// This code changes the parameter attributes,
// You can see the property change in the call code.
Function Update (PARAM)
{
// Change the attributes of an object;
// You can see the changes in the call code.
Param. Message = "I was changed ";
}
// Create an object and assign it to an attribute.
VaR OBJ = new object ();
OBJ. Message = "this is the original ";
// Call clobber and output obj. Message. Note that it does not change.
Clobber (OBJ );
Window. Alert (obj. Message); // still displays "this is the original ".
// Call update and output obj. Message. Note that it has been changed.
Update (OBJ );
Window. Alert (obj. Message); // display "I was changed ".
Test Data
When performing a value-based test, two distinct items are compared to see if they are equal. Generally, this comparison is performed by byte. When checking by reference, it is to see whether the two items are pointers to the same original item. If yes, the comparison result is phase
If not, the comparison results are not equal even if each byte contains exactly the same value.
Copying and passing strings by reference saves memory. However, the strings cannot be changed after they are created, so they can be compared by value. In this way, you can check whether two strings contain the same content, even if they are completely generated independently.