Hello, Make a Person
I had some difficulties with this exercice, because:
1- I thinked I had need three setters and three getter. So I refered me to this lesson which talk about this where the “set” and “get” are used: Use getters and setters to Control Access to an ObjectPassed
In this exercice it’s like “this.setxxxx” is a nother way to make a setter and I don’t remember in wich lesson we studied this.
Is it the good maner to do in the real life ?
Which way is the better between “set”, “get” and “this.nameOfMethode” ?
2- Aso I thinked that in generaly, functions (Methodes) had to add to the constructor’s proptotype as explain in this lesson:
" Functions are added to Bird’s prototype" Add Methods After Inheritance
If I consider setter and getter as Methode, Wouldn’t it have been better in real life to put the “this.methode” in the prototype ?
To summarize, I would like to understand how it would have been done in real life ?
Yeah, irl, I’ve never manipulated the getters/setters after the fact, always on the object prototype/class.
const obj = {
_myValue: 127,
get myValue() { return this._myValue; },
set myValue(newValue) { this._myValue = newValue; },
}
Or something like that. I think it’s good to know about the other method, but I’ve never had to use it or even seen it, at least not with getters/setters.
A couple of things. First of all, setters and getters came to JS after object constructors so their implementation is a little clunky. The class implementation has them built in and it is basically just a wrapper for object constructors.
Secondly, don’t put the word “get” and “set” in your getters and setters - it will mess it up - they should have the same name. And when you use them, you don’t access them as functions, you treat them like properties - JS knows what function to call behind the scenes.
This is how I would have implemented this:
class Person {
constructor(firstAndLast) {
this._fullName = firstAndLast;
}
get firstName() {
return this._fullName.split(" ")[0];
}
set firstName(name) {
this._fullName = name + " " + this._fullName.split(" ")[1];
}
get lastName() {
return this._fullName.split(" ")[1];
}
set lastName(name) {
this._fullName = this._fullName.split(" ")[0] + " " + name;
}
get fullName() {
return this._fullName;
}
set fullName(name) {
this._fullName = name;
}
};
var bob = new Person("Bob Ross");
console.log(bob.fullName); // Bob Ross
console.log(bob.firstName); // Bob
bob.firstName = 'Betsy';
console.log(bob.fullName); // Betsy Ross
I also would have stored the first and last name as separate variables - I think it is less work - but that is a matter of preference.
I wouldn’t even store “fullName” - I would just build it in the getter for that. I would just store first and last, not full. If you store as full, then there are 2 getters and 2 setters where you have to do some math. If you store them as individual names, then you one have 1 getter and 1 setter where you have to do math. It’s just more efficient. But that’s a nitpicky thing and maybe you don’t need to worry about it.