Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
How to access a property inside an object and use in another method? javascript

I need to create a method to return a customers name and bank account value like so:

*bank.printAccount('Sheldon'); // this should print 'Sheldon account is 0'*

However I am struggling to find out how to access the property customers to use in a method printAccount. Currently I have this:

class Bank {
    constructor(){
        this.customers = {};
    }
  
  }
  Bank.prototype.addCustomer = function(customer){
       this.customers[customer] = 0;

  }
  Bank.prototype.printAccount = function(){
  

  };
  var bank = new Bank();
  bank.addCustomer('Sheldon');
  bank.addCustomer('Bob');
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There you go:

class Bank {
  constructor() {
      this.customers = {};
  }
}
Bank.prototype.addCustomer = function(customer) {
     this.customers[customer] = 0;
}
Bank.prototype.printAccount = function(customer) {
     console.log(this.customers[customer])
};

var bank = new Bank();
bank.addCustomer('Sheldon');
bank.addCustomer('Bob');
bank.printAccount('Sheldon');
bank.printAccount('Bob');

You probably will need to add edge cases in printAccount() such as to check whether the customer exists or not.

Edit: Removed doubled initialization of customers.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda