3
I was doing some object orientation exercises and in the solution the teacher used set
and get
. I realized that even if I do it like the teacher, it doesn’t work. I can solve it without get
and set
, but wanted to know how to use them in Javascript.
class Endereco{
constructor(rua) {
this.rua = rua;
}
set novaRua(nova){
this.rua = nova;
}
let E1 = new Endereco(null);
E1.novaRua("rua x");
console.log(E1.rua);
The way out:
Uncaught TypeError: E1.novaRua is not a function at aula8.js:77
I think the only thing missing was the lock when he was transcribing the code for the question, otherwise the error he was going to receive would be
Uncaught SyntaxError: Unexpected identifier
, since he couldn’t even try to invoke (incorrectly) the Setter, since the parser would have failed earlier. :-)– Luiz Felipe
@Luizfelipe, probably yes, but I already solved a problem that was generated in the transcription of the question.
– Augusto Vasques