Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

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

Copy link
Copy link
Closed
@battmanz

Description

@battmanz
Issue body actions

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

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.