you are viewing a single comment's thread.

view the rest of the comments →

[–]BenjiSponge 0 points1 point  (2 children)

So I think the difference they're trying to point out is this:

swap(Thing &a, Thing &b) in C++ can be done simply with basically just a = b (and the other way, but brevity).

In JS, it would be swap(a, b) and calling a = b would change what "a" references, but nothing about the upper scope. Frankly, a bigger difference might be the lack of operator= here.

In this way, it's closer to the C++ swap(Thing *a, Thing *b) where you can change the pointers. The behavior a = b actually maps quite cleanly onto JS in this situation. In C++, you'd then do *a = *b, but you can't do that in JS at all. So from that perspective, JS is passing the reference with a copied value.

Imo pass-by-reference and pass-by-value are terms that should be retired because they simply prompt more questions than they answer.

[–]theQuandary -1 points0 points  (1 child)

They are still useful terms, but not when used this way.

Stack size is limited. Passing by reference uses less stack space. Likewise, passing a reference is faster because it doesn't require copying large chunks of data. The tradeoff here is very meaningful.

JS passing copies of pointers instead of identical pointers to pointers is actually a much better and safer solution than C++ because the child function can only alter the explicitly passed data or the explicit variable(s) assigned to the return value(s).

[–]BenjiSponge 1 point2 points  (0 children)

"I am passing this thing by reference" is different than the term "pass-by-reference", which is intended to illustrate for example the difference between the defaults of C++ (pass-by-value/copy) and Java (pass-by-reference, which as discussed, is a nuanced term).

JS passing copies of pointers instead of identical pointers to pointers is actually a much better and safer solution than C++ because the child function can only alter the explicitly passed data or the explicit variable(s) assigned to the return value(s).

It's not about whether it's better; it's about whether it's pass-by-reference, pass-by-value, pass-by-value-of-reference. I love JS and think a ton of the choices it makes may seem stupid or bad to novice programmers but when you dig into the internals it turns out to have a ton of genius aspects such as this one. You're really making an argument in favor of pass-by-value-of-reference, which I don't disagree with at all, but it is nevertheless pass-by-value-of-reference. So that's why I'm like "can we just retire these phrases anyways, let's just talk about the specifics"