Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Optimize _.defaults so that it doesn't access property values it doesn't need to #2983

@battmanz

Description

@battmanz

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions