copying, passing, and comparing data

Source: Internet
Author: User
Tags array arrays object comparison copy variables reference string
Compare | data

In JScript, the processing of data depends on the type of data.

Comparisons by value and by reference

Numbers and Boolean values (True and false) are copied, passed, and compared by value. When copied or passed by value, a space is allocated in the computer's memory and the original value is copied to it. Then, even if you change the original value, the copied value (and vice versa) will not be affected, because the two values are separate entities.

objects, arrays, and functions are copied, passed, and compared by reference. When copied or delivered by address, you actually create a pointer to the original item, and then use the pointer just like a copy. If you subsequently change the original item, the original item and the copy item are also changed (and vice versa). There is actually only one entity; "Copy" is not a real copy, but just another reference to that data.

When compared by reference, to be successful, two variables must refer to the exact same entity. For example, two different Array objects will be less equal even if they contain the same elements. To be more successful, one of the variables must be a reference for another. To check whether two arrays contain the same elements, compare the results of the toString () method.

Finally, strings are copied and passed by reference, but are compared by value. Note that if you have two string objects (created with new string ("something"), compare them by reference, but if one or both are string values, compare them by value.

Note in the order of the ASCII and ANSI character sets, uppercase letters are in the front of the lowercase letters in sequence sequence. For example, "Zoo" is less than "aardvark". If you want to perform a case-insensitive match, you can call toUpperCase () or tolowercase ()on two strings.

Passing arguments to a function

Passing a parameter to a function by value is a separate copy of the parameter, that is, a copy that exists only within that function. Even if you pass objects and arrays by reference, the new values are not reflected outside the function if you overwrite the original values directly with the new values in the function. This can be seen only when the attributes of an object or the elements of an array change.

For example (using IE object mode):

This snippet destroys (overrides) its parameters, so the  calling code does not reflect changes. {The      argument is broken;          param = new Object();    param.message = "This will not work";} This code changes the properties of the parameter, and  you can see the property changes in the calling code. function Update(param){      Change the object's properties;}      you can see the change from the calling code.}     param.message = "I was changed";} Creates an object and assigns it to an attribute. var obj = new Object();obj.message = "This is the original"; Called  Clobber , and output  obj.message . Note that it has not changed. Clobber(obj);  still displayed  "This is the original" . called  Update , and output  obj.message . Notice that it has been changed. Update(obj);  display  "I was changed" .

Test data

When checked by value, two distinct items are compared to see if they are equal. Typically, the comparison is done in bytes. When you check by reference, you see whether the two items are pointers to the same original item. If so, the comparison results are equal, and if they are not, the comparisons are unequal even if they contain exactly the same value for each byte.

Copying and passing strings by reference saves memory, but can be compared by value because it cannot be changed after the string is created. This allows you to check whether two strings contain the same content, even if they are completely independent.



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.