Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

The object is still passed by value, however the object itself contains references, which may be mutated. If objects were pass by reference then you’d end up with {a: 2} at the end:

    let a = {a: 1}
    
    function foo(a) {
      a = {a: 2}
    }
    
    foo(a)
    a //=> {a: 1}
Don’t confuse passing a mutable entity by value with pass by reference.



> The object is still passed by value, however the object itself contains references, which may be mutated.

This would imply that:

  var v = 1;

  (function (o) {
    o.a = 2;
  }({ a: v });

  console.log(v);  // => 2
The whole "objects are pass-reference-by-value" is obviously annoying and confusing, but we're kind of stuck with it.


Apparently there is another term (which I hadn't heard) which is: call-by-sharing

https://en.m.wikipedia.org/wiki/Evaluation_strategy#Call_by_...




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: