Closed
Closed
Copy link
Description
Suppose I have some config object that must be initialized. Well, at least some of the properties must be initialized -- some do not.
const initializableConfig = {
initialize(data) {
this.data = data;
},
get a() {
return this.data.a;
},
get b() {
return this.data.b;
}
c: "I don't need to be initialized"
};
I would like to be able to use the initializableConfig
as a default at all times (even before it's initialized). I should be fine to do so as long as I override the properties that must be initialized.
const config = {
a: 'apple',
b: 'banana'
};
const finalConfig = _.defaults({}, config, initializableConfig); // Error! Cannot read property 'a' of undefined
Since config
defines a
and b
, I see no reason why those properties need to be accessed on the initializableConfig
. Currently they are and an error is being thrown.
Couldn't _.defaults
be optimized to avoid this error?