I read through this post and I didn't quite understand why methods are important. The explanation states:
- They can be used to change object property values. The method setAge on line 4 allows us to update bob.age.
- They can be used to make calculations based on object properties.
Functions can only use parameters as an input, but methods can make
calculations with object properties. For example, we can calculate the
year bob was born based on his age with our getYearOfBirth method (line 8).
I understand how these situations work. What I don't quite get is how these benefits/uses are specific to methods.
I played around with the code and I was able to replicate the effects of benefits 1 without using the method setAge. Can't one simply input
to change his age. Why is a method required for this?
For the 2nd one, I see that getYearOfBirth can be more convenient, but I didn't think this was necessarily a great example of how methods can be used for calculations using object properties as in this instance, one could also say
instead of console.log(getYearOfBirth);
I was wondering if anyone could suggest a more poignant example(s) of how a method can calculate the object properties. I'm trying to think how this might come into play effectively, but not quite seeing the bigger picture.