You're replacing the object you're referring to with options
, but that has no effect on any reference to the original object the library has. There's nothing you can do with the return value of getOptions
that will replace the object the lib has with an entirely new object; all you can do is change the state of the object it gives you (adding, changing, and/or removing properties).
Unless the library provides you a way to change the object it refers to internally, you can't completely replace that object (without modifying the library).
Here's an example to illustrate:
// Emulating the library:
var lib = (function() {
var libOptions = {
name: "original"
};
return {
// The operation you said it has
getOptions: function() {
return libOptions;
},
// The operation it would need to have
setOptions: function(newOptions) {
libOptions = newOptions;
}
};
})();
var options = lib.getOptions();
options = {
name: "replaced"
};
console.log(options.name); // "replaced"
console.log(lib.getOptions().name); // "original"
lib.setOptions(options);
console.log(options.name); // "replaced"
console.log(lib.getOptions().name); // "replaced"
When the library gives you a reference to its object, you have something like this in memory:
libOptions−−+
|
| +−−−−−−−−−−−−−−−−−−+
+−−>| (object) |
| +−−−−−−−−−−−−−−−−−−+
| | name: "original" |
options−−−−−+ +−−−−−−−−−−−−−−−−−−+
Then when you set options
to something else:
+−−−−−−−−−−−−−−−−−−+
libOptions−−−−−>| (object) |
+−−−−−−−−−−−−−−−−−−+
| name: "original" |
+−−−−−−−−−−−−−−−−−−+
+−−−−−−−−−−−−−−−−−−+
options−−−−−−−−>| (object) |
+−−−−−−−−−−−−−−−−−−+
| name: "replaced" |
+−−−−−−−−−−−−−−−−−−+
As you can see, setting options
doesn't have any effect on the library's reference in libOptions
.
That's why the lib would have to provide setOptions
.
Of course, again, you can change the state of the object (provided the library isn't giving you a defensive copy or a frozen object);
options.name = "updated";
which does this:
libOptions−−+
|
| +−−−−−−−−−−−−−−−−−−+
+−−>| (object) |
| +−−−−−−−−−−−−−−−−−−+
| | name: "updated" |
options−−−−−+ +−−−−−−−−−−−−−−−−−−+