I\'ve got this code:
Object.assign
sets properties on the object you give it as its first argument; it also returns that same object. So in your first example, since you're passing option
as the first argument, it gets updated with new/updated properties. In your second example, you're not passing it as the first argument, it's just one of the "source" objects to read properties from, so it isn't updated.
If your confusion is why the assignment didn't change opts
, it's because assigning to the parameter doesn't have any effect on anything outside the function. E.g.:
function foo(a) {
a = 42;
}
var x = 67;
foo(x);
console.log(x); // Still 67
That's because foo(x)
reads the value of x
and passes it into foo
. There is no connection between a
and x
other than that a
's value originally came from x
.
It's exactly the same with opts
/options
. mergeOptions(opts, passedOptions)
reads the value of opts
, which is an object reference, and passes that value into mergeOptions
. There's no ongoing connection between that value and opts
. The object reference points to the object, not to the variable opts
.