Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
battmanz opened this issue Feb 8, 2017 · 5 comments
Labels

Comments

@battmanz
Copy link

battmanz commented Feb 8, 2017

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?

@jdalton jdalton added the bug label Feb 8, 2017
@jdalton jdalton closed this as completed in efa68ba Feb 8, 2017
@jdalton
Copy link
Member

jdalton commented Feb 8, 2017

Yep, that's doable. Thanks!

jdalton added a commit that referenced this issue Feb 8, 2017
@battmanz
Copy link
Author

battmanz commented Feb 8, 2017

Wow, that was fast! Thank you!

@battmanz
Copy link
Author

battmanz commented Feb 9, 2017

@jdalton Do you know when the next release will be?

@jdalton
Copy link
Member

jdalton commented Feb 9, 2017

It'll be in our v5 release, so expect a delay.

@lock
Copy link

lock bot commented Dec 27, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Dec 27, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Development

No branches or pull requests

2 participants