Why javascript "for loop" has different types of behavior for objects of different types

I understand that JavaScript has no pointers, however, I noticed this "pointer" behavior when going through arrays containing objects, but not a similar behavior when the array contains numbers (for example).

var ARR_num = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; for (var i = 0, len = ARR_num.length; i < len; i++) { var item = ARR_num[i]; item++; } console.log(ARR_num); //Outputs [0,1,2,3,4,5,6,7,8,9] 

Now with an array with objects

 var ARR_obj = [{}, {}, {}]; for (var i = 0, len = ARR_obj.length; i < len; i++) { var item = ARR_obj[i]; item.pointer = true; } console.log(ARR_obj); //Outputs [{pointer: true}, {pointer: true}, {pointer: true}] 

Why are these two different behaviors?

+5
source share
4 answers

Why are these two different behaviors?

when you assign an object to another variable, the new variable points to the same objects, therefore, when the properties of the new variables change, the objects become mutated Example:

 var a= {name: "some"}; var b = a; b.name = "newName"; console.log(a.name);// "newName" 

when you assign a primitive type to another variable, its just a new variable and you don't refer to the old variable, so changing the new variable will not affect the old one. Example:

 var a = "some"; var b = a; b = "newName"; console.log(a);//"some" 

hope this helps!

+1
source

Why are these two different behaviors?

Because you have two different operations. In the latter case, you mutate the state of the object

 item.pointer = true; 

item continues to point to the same item. Only the internal elements of the object are changed.

But here

 item++; # equivalent to item = item + 1 

you reassign . After the operation, he will point to another integer. If you perform similar operations in an object loop

 item = { pointer: true } 

Then your original array elements will not be affected, and you will not see any "pointer semantics".

And yes, this also has something to do with goals, which are primitive / immediate meanings.

0
source

{} is the literal syntax of an object that creates (in your case) new objects in the array. When you go through it and use ARR_obj [i], you get a reference object, which is then processed.

Or put it in other (more general) words:

Primitives (undefined, null, boolean, string and number) are passed by value, while objects are passed by reference (or, more precisely, a copy of the link).

0
source

As you know, all values ​​(which are not primitives) are pointers to objects. Thus, the element is a pointer to the object in use case 2, where, as in the case of use, 1 is its integer.

In usage example 2, referring to the value "true" for the entire iteration. since the code says that it gives the correct result.

In general, JavaScript does not support parameters for passing by reference, therefore, using objects in Javascript, we can pass the link.

0
source

Source: https://habr.com/ru/post/1271050/


All Articles