Calling a method within a Javascript method

Asked

Viewed 246 times

0

Follows the class:

class Advogado{
        constructor(nomeAdv, tipoEnderecoAdv, ruaAdv, numAvd, cidadeAdv, estadoAdv, oabADV, estadoOabAdv, emailAdv){
          this.nomeAdv = nomeAdv;
          this.tipoEndereco = tipoEnderecoAdv;
          this.ruaAdv = ruaAdv;
          this.numAvd = numAvd;
          this.cidadeAdv = cidadeAdv;
          this.estadoAdv = estadoAdv;
          this.oabADV = oabADV;
          this.estadoOabAdv = estadoOabAdv;
          this.emailAdv = emailAdv;


        }
         geraEnderecoAdv(){
           return `${this.tipoEnderecoAdv} ${this.ruaAdv}, nº ${this.numAvd}, na cidade de ${this.cidadeAdv} - ${this.estadoAdv}`;
         }

         dizOutorgado (){
           return `${this.nomeAdv}, brasileiro(a), inscrito(a) na OAB/${this.estadoOabAdv} nº ${this.oabADV}, com escritório na ${this.tipoEndereco} ${geraenderecoAdv()} e email ${this.emailAdv}, onde recebe intimações`;
         }

      }

How do I make the Autorrgado() method, which calls the geraEnderecoAdv() work? It always says that the called method is nonexistent.

One more thing. If I turn the object created in this class into a Json with JSON.stringify and then recover it I will lose the methods by keeping only the key/value pairs?

2 answers

0

By the code you put on it, it’s general addressAdv() and not geraEnderecoAdv(). Then you’ll have to put the this in the method call. See if it works. If it doesn’t, try changing the methods to Arrow functions.

  • 1

    Bro, how dumb I am... I felt like it was something basic. Didn’t even need to use the functions Arrow... Thanks!

0

Just to register an example on the site, you need to use the this to reference the method and also concatenate to other texts.

class Advogado {
  constructor(nomeAdv, tipoEnderecoAdv, ruaAdv, numAvd, cidadeAdv, estadoAdv, oabADV, estadoOabAdv, emailAdv) {
    this.nomeAdv = nomeAdv;
    this.tipoEnderecoAdv = tipoEnderecoAdv;
    this.ruaAdv = ruaAdv;
    this.numAvd = numAvd;
    this.cidadeAdv = cidadeAdv;
    this.estadoAdv = estadoAdv;
    this.oabADV = oabADV;
    this.estadoOabAdv = estadoOabAdv;
    this.emailAdv = emailAdv;


  }
  geraEnderecoAdv() {
    return `${this.tipoEnderecoAdv} ${this.ruaAdv}, nº ${this.numAvd}, na cidade de ${this.cidadeAdv} - ${this.estadoAdv}`;
  }

  dizOutorgado() {
    return `${this.nomeAdv}, brasileiro(a), inscrito(a) na OAB/${this.estadoOabAdv} nº ${this.oabADV}, com escritório na `+  this.geraEnderecoAdv()+ ` e email ${this.emailAdv}, onde recebe intimações`;
  }

}

let Adv = new Advogado('Marcelo Flemmingham', 'Avenida', 'Roberto Frizzo', 404, 'São Paulo', 'SP', 1231212121, 'RJ', '[email protected]')

document.write(Adv.dizOutorgado());

Now as for your second question, it is not clear, because when the class instance, it will generate a object, with class attributes plus data filled in the class instantiation, if you give a console.log(JSON.stringify(Adv)) will only transform that object class instance to string, but, the class continues with her normal methods, I think you made some kind of mess there.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.