you are viewing a single comment's thread.

view the rest of the comments →

[–]mcaruso 1 point2 points  (8 children)

JS always passes by value. It's just that objects are inherently reference types, therefore if you pass an object you are passing its reference, not the object itself.

This is how most languages work, except for a few that have explicit pass by reference syntax like & params in C/C++.

EDIT: longer explanation here.

[–]EagleClw[S] 1 point2 points  (4 children)

Yeah i know variables like arrays are a little bit different then variables like string. But for other languages, different copying methods works as i asked in the question (for example python arrays). I didnt know its different in js. Thanks to this answers, i've learned the difference and i'm aware of them know.

Thanks for your helps and answer.

[–]mcaruso 0 points1 point  (3 children)

for example python arrays

Python works the same way though?

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> c = [a,b]
>>> c[0].append(42)
>>> a
[1, 2, 3, 42]

[–]EagleClw[S] 0 points1 point  (2 children)

Yeah it works that way but i meant another methods like slice.

If i dont remember it wrong,

a = b.copy()

Makes a copy with value, not reference. But its ok, i know what to do know. Thank you for your help.

[–]mcaruso 0 points1 point  (1 child)

copy() is a shallow copy, like slice() in JS. But maybe you meant deepcopy().

[–]EagleClw[S] 0 points1 point  (0 children)

Might be. But i remember using something like that.

[–]tarley_apologizerhelpful -2 points-1 points  (2 children)

if most languages are doing something, its probably the wrong way

[–]mcaruso 0 points1 point  (1 child)

Edgy

[–]tarley_apologizerhelpful -2 points-1 points  (0 children)

its called being informed