1

(Sorry for my bad English)

I am new to JS and currently on the Object/method part!

I've just written this pieces of code:

var jason = {
  fname: 'jason',
  lname: 'roy',
  yearBorn: 2001,
  CalcAge: function () {
    this.age = 2020 - this.yearBorn;
  }
};
jason.CalcAge();
console.log(jason.age);

The expected result on console log is 19 and that's exactly what I'm getting! But that's not my question.

My question is what is the point of going through such a long process just to create an age property?

AS you can, to print out the "age: 19" we first need to write:

jason.CalcAge();

And then,

console.log(jason.age);

Dont you guys think its kinda useless?

Like, I'm pretty sure you can simply create an age property and write function there to do the same thing as we're doing here but by doing an extra step of calling a function and then console logging the property it generates.

Once again, sorry for my bad English and let me know if you guys didn't get me!

1
  • "I've just written this pieces of code" - how would we know why *you went through such a long process? :-P You might have avoided the method and just written jason.age = 2020 - jason.yearBorn; instead of calling the method, or you might have dropped the .age property completely and just written console.log(2020 - jason.yearBorn). However, the standard approach would be probably be to give the object a getAge() method that can be called and returns the calculated age instead of assigning it to a property. Commented Aug 2, 2020 at 17:21

1 Answer 1

1

Ciao, an object with a method like yours:

var jason = {
  fname: 'jason',
  lname: 'roy',
  yearBorn: 2001,
  CalcAge: function () {
    this.age = 2020 - this.yearBorn;
  }
};
jason.CalcAge();
console.log(jason.age);

And and object without a method like this:

var jason = {
  fname: 'jason',
  lname: 'roy',
  yearBorn: 2001,
  age: 19
};
console.log(jason.age);

seems similar but are totally different. Why? Flexibility. Let me rewrite a little bit your object:

var jason = {
   fname: 'jason',
   lname: 'roy',
   yearBorn: 2001,
   CalcAge: function (currYear) {
      this.age = currYear - this.yearBorn;
   }
};
jason.CalcAge(2020);
console.log(jason.age);

As you can see, now I'm passing the current year as parameter of function CalcAge. What's the difference between the 2 solutions? First one is static (age: 19) and next year will be no more valid. Second one allows you to calculate age in any year you want (not only current year). So your solution (with a little modification) becames more flexible and could be used forever.

Sign up to request clarification or add additional context in comments.

3 Comments

Uh, age: 2020 - this.yearBorn doesn't work (and the semicolon is a syntax error)
Thanks @Bergi. I was focused on how is important to make code flexible that I made a stupid mistake.
No problem. If you think my answer was useful, please mark it as correct answer. I would really appriciate it. Have a nice day :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.