0

Let's consider this:

var obj = {a: 'tony'};
someFn(obj);

and this:

someFn({a: 'tony'});

Since we know that 'obj' is a reference, "{a: 'tony'}" is an object literal, is there any difference between those two ways of passing an argument?

Tony
  • 153
  • 1
  • 8

1 Answers1

0

What's the difference between pass-by-value and pass-by-reference in JavaScript?

That there is no pass-by-reference in JavaScript, only pass-by-value.

Since we know that 'obj' is a reference…

No, obj is a variable. And there are no references to variables that can be passed around. You always pass the value of the variable in a call like someFn(obj).

However, the variable might hold an object reference as its value, and that is indeed the value that is passed here. Which will allow the function someFn to mutate the object in place, but not assign to the variable obj.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Since you pass not the reference itself but a value that represents a copy of a reference, this evaluation strategy is sometimes called `pass-by-sharing`. –  Feb 04 '17 at 13:28
  • Yes, it's a copy of the reference value, so the object is shared. – Bergi Feb 04 '17 at 13:33