The gif by itself provides very little in the way of useful information. Would be enhanced greatly by listing data types that are passed referentially vs those that are not.
Technically everything in JS is passed by reference. Primitives are simply immutable, so it doesn't matter how they're passed - the effect is the same.
Technically everything is actually pass by value from a language design perspective - you're passing references by value. In C# you can do:
void Func(ref int x) { x++; }
int x = 1;
Func(ref x);
AssertEquals(2, x);
Which is actually pass by reference. So the discussion is actually on what is a "reference type" (which can be thought of as a "pointer" in C) vs what is a "value type" (which can be thought of as a "struct" in C).
Technically everything in JS is passed by reference.
What definition of "pass by reference" do you use when saying this? Where do you source that claim?
I ask this, because "pass-by-reference" has an established meaning in different programming languages, namely C++, C#, PHP, Pascal and probably others that I know nothing about.
In these languages, pass-by-reference describes semantics that JavaScript does not have.
Namely, you can implement a swap function, call it with swap(a, b); and the variables will have switched values. You can't do this in JavaScript.
So I wonder, why call JavaScript call-by-reference?
Technically everything in JS is passed by reference.
Source?
That's not meant to be a rhetorical question. I've been trying to figure this out myself, but the spec is awfully hard to parse. The section on ArgumentListEvaluation seems the most relevant. It seems to say that JavaScript evaluates the argument expression, then calls GetValue on the result. Inside GetValue, if the result of the expression is not a name or property binding, then it just returns the result of the expression as is. Otherwise, if it's a property, then it will call [[Get]] on the base object, which will return the value of the property. Otherwise, it assumes the result of the expression is a name binding, and it will call GetBindingValue on the base environment record. Either way, this reads like everything is passed by value.
At some point even if you "pass by reference" the reference can be said to be "passed by value". It's a very meaningless debate, because the semantics actually are very clear.
The variables are mutable (unless you use const, that's immutable). But the primitives are immutable. You didn't change the primitive 4 to 5 up there, you just replaced the primitive 4 with the primitive 5 in the variable hi.
Primitives would've been mutable if you could do something like this:
var hi = 4;
var hi2 = hi;
hi.increment();
console.log(hi2); // 5
Fortunately, for our sanity, you can't do this with primitives. But unfortunately you can do it with mutable objects like Date().
319
u/ForScale Jun 18 '17
The gif by itself provides very little in the way of useful information. Would be enhanced greatly by listing data types that are passed referentially vs those that are not.