Ie9 ff11, a bug in JavaScript Array Processing

Source: Internet
Author: User
Tags javascript array

I also know if it is a bug or is it like this?

View JS Code:

 

VaR Ep = {Title: "Default album", Desc: "Come and upload a photo", IMG: "http: // pic. ** m..403d0c295512c740_mb.jpg ", link:" u1194252496/Photo/show/82354307 ", title:" Default album "} var list_photo = [] list_photo.push (EP) list_photo.push (EP) list_photo [1]. IMG = URL + '? '+ Idalert (list_photo [2]. IMG) Alert (list_photo [1]. IMG)

The two values displayed in the result are the same. In fact, all values are modified.

 

 

I found the answer in a very old CHM (script56.chm) document:

In JScript, data processing depends on the data type.

Compare by value and by reference

Values of the numbers and Boolean types (TrueAndFalse) Is 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, the copied value is not affected (in turn), because these two values 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 you subsequently change the original item, both the original item and the copy item will be changed (the same way in turn ). 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 differentArrayObjects that contain the same elements are not equal. To be successful, one of the variables must be another reference. To check whether two arrays contain the same elements, compareTostring ()Result of the method.

Finally, strings are copied and transmitted by reference, but compared by value. Note that if there are two
String
Object (UseNewString ("something"), compare them by reference. However, if one or both of them are string values, compare them by value.

(The test found that the string is not copied and transmitted by reference)

Note: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 callTouppercase ()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. It can only be seen outside the function when the attributes of the object or the elements of the array change.

For example (using the IE Object Mode ):

// This code segment destroys (overwrites) its parameters, so // The Call Code does not reflect any changes. Function clobber (PARAM) {// destroy the parameter; // It cannot be seen in the call code. Param = new object (); Param. Message = "This will not work";} // This code changes the attribute of the parameter. // you can see the attribute change in the call code. Function Update (PARAM) {// Changes the object attributes; // 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 equal. If not, the comparison result is not equal even if each byte contains 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.

 

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.