Home > OS >  How to calculate age on Class when creating an instance
How to calculate age on Class when creating an instance

Time:11-13

I would like to automatically calculate and set an age when creating an instance. I made a function that calculates and returns an age. It works, but I wonder if it's possible to write the function/method in the Member Class without using the global space.

class Member {
  static id = 0;

  constructor(firstName, lastName, birthDay) {
    Member.id  ;
    this.id = Member.id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = birthDay;
    this.age = getAge(birthDay);
  }
}

const m1 = new Member('Oliver', 'Cruz', '11/13/1990');
console.log('m1:', m1.age); // 32 (as of Nov 13rd, 2022)

const m2 = new Member('Sophia', 'Brown', '11/30/1992');
console.log('m2:', m2.age); // 29 (as of Nov 13rd, 2022)

/**
 * Calculate age function
 * @param {String} birthDay - ex '11/13/1990'
 * @returns {Number} - age
 */
function getAge(birthDay) {
  const now = new Date();
  const bd = new Date(birthDay);
  const diff = now - bd;
  const age = new Date(diff).getFullYear() - 1970;

  return age;
}

CodePudding user response:

You can add it as a static method on the Member class.

class Member {
  static id = 0;

  constructor(firstName, lastName, birthDay) {
    Member.id  ;
    this.id = Member.id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = birthDay;
    this.age = Member.getAge(birthDay);
  }

  /**
   * Calculate age function
   * @param {String} birthDay - ex '11/13/1990'
   * @returns {Number} - age
   */
  static getAge = (birthDay) => {
    const now = new Date();
    const bd = new Date(birthDay);
    const diff = now - bd;
    const age = new Date(diff).getFullYear() - 1970;

    return age;
  }
}

const m1 = new Member('Oliver', 'Cruz', '11/13/1990');
console.log('m1:', m1.age); // 32 (as of Nov 13rd, 2022)

const m2 = new Member('Sophia', 'Brown', '11/30/1992');
console.log('m2:', m2.age); // 29 (as of Nov 13rd, 2022)

CodePudding user response:

class Member {
  static id = 0;

  static getAge(birthDay) {
     const now = new Date();
     const bd = new Date(birthDay);
     const diff = now - bd;
     const age = new Date(diff).getFullYear() - 1970;

     return age;
  }

  constructor(firstName, lastName, birthDay) {
    Member.id  ;
    this.id = Member.id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = birthDay;
    this.age = Member.getAge(birthDay);
  }
}

?

Not quite sure how you want to be able to access getAge. I made it a static method so you can access getAge from wherever without having to make a separate Member instance, but it's up to you. If you want to make it a class method, just do

//...
getAge(birthDay) {
  //...
}
//...

constructor(/* args */) {
  //...
  this.age = this.getAge(birthDay);
  //...
}

I just realized there's two other responses. L for me

CodePudding user response:

You can set the function as a method within the class

class Member {
  static id = 0;

  constructor(firstName, lastName, birthDay) {
    Member.id  ;
    this.id = Member.id;
    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDay = birthDay;
    this.age = this.getAge(birthDay);
  }
  getAge(birthDay) {
    const now = new Date();
    const bd = new Date(birthDay);
    const diff = now - bd;
    const age = new Date(diff).getFullYear() - 1970;

    return age;
  }
}
const m2 = new Member('Sophia', 'Brown', '11/30/1992');
console.log('m2:', m2.age);

Now there is a method for each instance instead of one function in the global scope.

  • Related