DEV Community


Discussion on: Troll Hunting 101: JavaScript Passes Objects by Reference

functional_js profile image
Functional Javascript • Edited

Ha ha. I know the troll you're talking about.
They have nothing better to do than waste everybody's time with hyper-intensive bikeshedding.

Just like you said, in Javascript, like C# and Java, you can create new variables that point to reference types (heap objects). (regardless of whether you pass them into a func or not).

Passing a reference type into a func simply allocates a new stack frame to the heap object (the referenced object on the heap). We programmers call this pass by reference, because the heap object itself is not copied. We've passed a new reference to the heap object. If the trolls want to call it something else then go ahead. You do you.

Other languages like C# have a ref keyword for "passing reference types by reference". This also allocates a new variable on the stack just like "pass by reference", but points to the other stack variable, which points to the heap object.
Of course nobody is saying this "allocate stack reference to a stack reference" exists in JavaScript, but the trolls want to troll and try to trick their victims into saying they meant that.

Keep up the good work Adam.

Forem Open with the Forem app