Re:Deep-copying in JavaScript
by zzzcpan on Thursday January 25, 2018 @03:42PM
(#16234458)
From a user perspective most languages make a mess out of these things and confuse people. In Javascript certain things are declared in a similar way, but some behave like values and some behave like references. Which makes it both pass-by-value and pass-by-reference.
[ Parent
| on HN ]
Wrong from CS point of view, references are also pass-by-value on JavaScript.
If JavaScript allowed pass-by-referece, we would be able to do the following,
function swap(x, y) {
let c = x
x = y
y = c
}
let a = {a: 6, b: 7}
let b = {c: 8, d: 8}
swap(a, b)
console.log(b)
// Object {a: 6, b: 7}
Meaning, pass-by-reference means being able to actually change the "pointer" used by the local variable.I understand what you mean, but consider this to be an incorrect view on these things. They are not about CS, but about user experience.
The incorrect view is to teach wrong concepts just because some people rather learn based on gut feeling, than taking the effort to learn it properly.
I believe that javascript always uses pass-reference-by-value, but simple types are immutable, which means that pass-reference-by-value is functionally identical to pass-by-value since there is no way to make changes to the immutable referent.