DEV Community

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

Collapse
 
iquardt profile image
Iven Marquardt

I think (but am not sure) that Javascript actually pursues a call-by-sharing strategy for reference types (aka object types). With call-by-reference the callee should be able to reassign the caller's variable, right? Here is an example:

foo = xs => {
  xs = [1];
  return xs;
};

let xs = [];

foo(xs);
xs; // []

With call-by-ref xs should be [1], because the actual reference of the variable is passed. So what passes call-by-sharing then? Frankly, I don't know. I read somewhere it would only pass a copy of the reference. This doesn't make much sense to me though. A copy of a pointer to memory?

Anyway, this is just nitpicking. Essentially Javascript uses call-by-ref.