0

in this example, it seems that we can use a variable (here "second") to fill the array myArray, as if second was a "reference" to myArray : is that really what happens here?

var myArray = [];
var second = myArray;
    second.target = … //we fill the "second" variable
    second.offsetX = …
    second.offsetY = …

var target = myArray.target; //then we retrieve the result from myArray
if (target) {

Thanks

4 Answers 4

2

second was a "reference" to myArray : is that really what happens here?

Yes.

Objects—like arrays—in JavaScript are passed and assigned by reference.

From your example, myArray and second both point to the same object in memory.

Sign up to request clarification or add additional context in comments.

Comments

1

Yes, this is exactly what happens here. When you (for example) push new elements to second, you can read them later from myArray.

BTW, I sense that you're doing something strange. Why do you set an offsetX on an array?

1 Comment

You are correct. I think, he is just testing the object property. @Paul, offsetX and offsetY are mouse position related properties. So, it is better if we avoid using the same property names, which are already defined in java script.
1

This is called a shallow copy. You have a reference (var second = ...) to the original array (var myArray = ...), they both are pointing to the same memory in the memory of the JavaScript virtual machine.

This way you can access the array either by second or myArray.

Comments

1
var myArray = []; 

This is just an array declaration It is same as var myArray=new Array();

About Array Referencing:

var second = myArray; 

We are pointing the variable second to myArray memory location. Here new Object second will be created point to content of myArray. So, if you read content of second. It will read the myArray. But, you edit/update the content of second, content of myArray will be copied into second and it will be modified. As Bakudan said, It is the shallow copy. See the example below,

var myArray=[10,20,30];
var second =myArray; //second will contain 23,45 and 100.

If we update the array second, second=[100,200,300] Original contents will be cleaned and 100,200,300 will be written.

To append the content to array second without removing the original content, We need to use function push as below:

second.push(100);second.push(200),second.push(300);

Now, content of second will be 10,20,30,100,200,300.

Object Property:

second.target = "testString";
second.offsetX =87;
second.offsetY =56;

This is the creation of object properties. It is same as,

second={"target":"testString","offsetX":87,"offsetY":56};

If you want to access value 87, it can be accessed as second.offsetX or second[offsetX].

More Information about java script Array is available here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.