Using another class variable in Javascript

Asked

Viewed 280 times

-3

I have the following class;

class Banco {

constructor (agencia, conta, saldo) {
    
    this.agencia = agencia
    this.conta = conta
    this.saldo = saldo


exibir () {

    console.log(this.saldo)

 }

}

Let’s say I want to use the agency variable in another class

class novoBanco extends Banco {

    exibir_agencia () {

        console.log(this.agencia)
        
    }

}

In this above scenario, I still need to pass the variable as a parameter of the new class:

var exemplo0 = new Banco('1111-1', '22222-2', 0)
var exemplo = new novoBanco('1111-1')
exemplo.exibirAgencia()

Is there any way to use this variable in another class without having to pass it as parameter of the new class?

  • Making the correction in "...Digamos que eu queira utilizar a variável agencia em outra classe..." and "...Existe alguma forma de utilizar essa variável em outra classe..." the appropriate name for that structure intended to access a data hosted in a class or object whose membership is called Estate.

2 answers

2

There is the conceptual error, or the question does not make sense. It has another class, but has no other object. It would be good to read What is the difference between a class and an object? to better understand.

Inheritance is a way of creating a new class, that is, a model, without having to specify everything that already exists in the mother class and serves well for the daughter class. It has nothing to do with harnessing data in the object, and in fact it doesn’t even make much sense. Maybe I didn’t quite understand how inheritance works, because I would use it, even if it hardly makes sense of truth, I’ll consider it just to test the mechanism.

When you are creating an object and storing in exemplo is only dealing with the daughter class. further, there is no variable passing anywhere.

Strictly should not only pass the agency. In fact a coreto code should test this and not accept if the other data is missing.

There seem to be other conceptual errors but I won’t talk because I have no more information.

The most organized code (pay attention to the details)):

class Banco {
    constructor(agencia, conta, saldo) {
        this.agencia = agencia;
        this.conta = conta;
        this.saldo = saldo;
    }
    exibir() {
        console.log(this.saldo);
    }
}

class NovoBanco extends Banco {
    exibirAgencia() {
        console.log(this.agencia);
    }
}

var exemplo = new NovoBanco('1111-1');
exemplo.exibirAgencia();

I put in the Github for future reference.

  • Then of course the object would be created for the 1st class, already passing (agency, account and salary). The question is how to use the variable "agency", in the class "Novobanco", without having to pass it as parameter of the object "example", because it would already be passed as parameter of the object of the class "Database"

  • None of this makes any sense, and I don’t think you read what I said even after I told you that your whole concept is wrong you’re repeating it.

  • The question is not whether it is right or wrong, the concept I understand ! I just want to know whether it is possible or not, it’s simple ...

  • If you’re wrong, you can’t. You still don’t understand what programming is, I suggest changing the way you want to do it.

  • Okay, thank you !!!

-2


You can use the concept of getters and setters to persist the data. in a very basic way follows an example below using static class. but this is not the best way to do it, but only to show that it is possible, and realize that the Bank Class is no longer extended.

For if you extend Banco in NovoBanco the variable agencia will be reset and returns undefined because when extending Bank, the builder will renew the past values, if nothing is informed the return will be Undefined.

static class Foo { 
  public static agencia;

  static setAgencia(a) {
    self.agencia = a;
  }
 
  static getAgencia() {
    return self.agencia;
  }
}


class Banco {
   public data;
   
   constructor(agencia, conta, saldo ) {
        this.agencia = agencia;
        this.conta = conta;
        this.saldo = saldo; 
        this.data = Foo.setAgencia(this.agencia);
   }

   exibir() {
       console.log(this.saldo,Foo.getAgencia());
   }
}

class NovoBanco  {
  exibirAgencia() {
       console.log('Exibindo agencia\nGravada em new Banco():\n Angecia: ',Foo.getAgencia());
  }
}

var getConta = new Banco('1234-5', '22345', 0)
var exemplo = new NovoBanco();
exemplo.exibirAgencia();

But pay attention to the concepts of OOP. as @Mineiro explained.

  • I tried to show the example as typescript, but I couldn’t add the library, so follow the link with example: https://jsfiddle.net/3jL4cput/

  • 1

    That’s exactly what I wanted. Thank you !

  • That doesn’t make any sense, the code doesn’t run.

  • Try here https://jsfiddle.net/3jL4cput/

  • This wheel, but it still makes no sense, broke what should be heritage, created a class so meaningless that it has no useful name.

  • 1

    I understand, and yes, because with inheritance could not do, because as you said does not make sense, the class without a useful name was a way to show that it was possible to do what was questioned, and in a clear way that is possible, I do not judge anyone by the way of programming , I’m just trying to help in the best way I can, I don’t want to drag this out, but the one thing I don’t understand is your frustration.. look on the bright side, problem solved, how it will be developed is already another issue the paths of the stones were given, and there are various documentation for research.

Show 1 more comment

Browser other questions tagged

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