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

186
Vistas
Why does my last customer entry override the previous entry instead of adding it to the customer object? JS

Why does my last customer entry override the previous entry instead of adding it to the customer object? JS Any help would be much appreciated.

class Bank {
    constructor() {
        this.customers = {};


    }
}

Bank.prototype.addCustomer = function (customer) {
    this.customers = customer;
    return this;

}
var bank = new Bank();
var newCustomer1 = bank.addCustomer('Curtis');
var newCustomer2 = bank.addCustomer('Alice');

console.log(bank.customers);

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

First, customers is not an array, you have declared it as a Javascript Object. Second, when you change it to an array, use the push method to add elements to the end of the array.

I've changed your code accordingly.

class Bank {
    constructor() {
        // Change 2
        this.customers = [];


    }
}

// Change 2
Bank.prototype.addCustomer = function (customer) {
    this.customers.push(customer);

}
var bank = new Bank();
bank.addCustomer('Curtis'); 
bank.addCustomer('Alice');
console.log(bank.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