In the last section, we learned about the concept of setters and getters. Now adapt that knowledge and update your previously written Person class to make use of this concept.
Recap of advantages
Setters allow you to…
- check the validity of values
- update other state attributes
- assign values like to attributes with the equality (=) symbol
Getters allow you to…
- calculate values from other state attributes
- use values like attributes in expressions without method brackets
Both…
- can improve the readability of code, when using the class
- can hide background complexity
Disadvantages
Of course the sun is not always shining.
- You write more lines of code in your class.
- misspelling in the use of setters won’t cause an error
- it will extend the instance objects with a new attribute of the wrong written setter name
- a typo in a method name instead would cause “typoInSettler() is not a function”.
Don’t hesitate to assign a value to an instance of your rewritten person class, that does not exist. Like forgetting the R in firstName.
var myPerson = new Person('Maks','Nemisj','2016-06-07');
myPerson.fistName = 'Maik';
console.log(myPerson);
It can be avoided with Proxies, but that solution is more expert class than introduction. See here: https://dzone.com/articles/why-getters-and-setters-are-a-bad-idea-in-javascri
Now we saw how to produce setters and getters and how to use them. Let’s move forward the the concept of inheritance.