A little discovery of array applications in JavaScript

Source: Internet
Author: User
Tags define array empty reference javascript array
Javascript| array today, using JavaScript to write a small program, using an array of a small discovery, turned the book at hand did not introduce, so sent over the shortcoming.

First we define an array and assign it a value, and then define an empty array, another empty array = the original array:

/***************** Code Snippet a *****************

var array1 = new Array ("A", "B", "C");

var array2 = new Array ();

Array2 = Array1;

/******************************************

My intention was to use the new array to temporarily store the original array data, because I could change the data, but I didn't want to change the original array, so I used a new temporary array to store the data. But the result of the operation was that I manipulated the new array, and the value of the original number also changed.

/***************** Code Snippet two *****************

Array2.pop ();

Array2.push ("D");

var tempstr = "";

for (var i=0; i<=array1.length; i++)

TempStr + = Array1[i];

alert (TEMPSTR);

/******************************************

It reminded me of the references or pointers in C/s + +, but I did not mention the concept by rummaging through the various JavaScript manuals or tutorials at hand, but the new array is indeed a reference to the original array. The other way to initialize the new array does not occur:

/**************** Code Snippet three ******************

var array1 = new Array ("A", "B", "C");

var array2 = new Array (array1);

/******************************************

Some think: Win under the JavaScript of the array object should be VC + + write class, and the author does not overload "=", so that the JavaScript array directly with "=" assignment to become a reference to the source array.



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.