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?
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?
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
.