1

Hello I have the following code:

export default class UserData {
  constructor(days = {}, firstDay = getFirstDay(), lastAccessDay){
    this.days = days;
    this.firstDay = firstDay || getFirstDay();
    this.lastAccessDay = lastAccessDay;
  }

  static getFirstDay() {
    return new Date();
  }
}

The idea was that if I don't pass a value to the constructor it would use a static method to calculate the firstDay.

The problem is that to the constructor getFirstDay() is undefined.

I'm a bit confused about what would the best way to set this up following best practices and conventions.

Enrico Polanski
  • 162
  • 1
  • 9
  • Yes, the variable `getFirstDay` is undefined. It's a static method. – Bergi May 11 '18 at 16:34
  • You should call it either as the default parameter value *or* behind the `||` operator, not in both places. – Bergi May 11 '18 at 16:35
  • So how would I fix this and pass to the constructor a computed value with a method inside the class? @Bergi – Enrico Polanski May 11 '18 at 16:37
  • getFirstDay is undefined, but UserData.getFirstDay **is not** and can be used in the constructor as a default param. it's a little different than i've seen in standard practice, but it's still possible. – worc May 11 '18 at 16:39
  • @EnricoPolanski Use the class to access the method: `constructor(days = {}, firstDay = UserData.getFirstDay(), lastAccessDay) { … }`. See the duplicate for details. – Bergi May 11 '18 at 16:43
  • @Bergi dude, that's the answer i was going to post. – worc May 11 '18 at 16:44
  • not a duplicate. – worc May 11 '18 at 18:01

0 Answers0