Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

322
Visualizações
Javascript, inherit attribute and method of objects

I got this javascript exercise. I made the Customer object and inherit all of the attributes from Person objects. At first, I got the error which says there is no "name" function in Customer. so I tried if I could get "firstname" from Customer object, but it also didn't work and returned the error saying there is no "customer1.firstname is not a function".

I googled and I think call function should inherit the attributes("firstname" and "lastname", also "name" method) into Customer object. I'd be glad if you help me out with this.

Thank you in advance!


Write a definition for the Customer objects. Customer has to inherit Person. In addition to the inherited attributes, Customer needs to have "id" and "spent", which represents the amount of money the customer has already used in the specific store. If a value for "spent" is not included when creating the object, its value should be 0. Additionally, the Customer objects should have a method "bonus", which checks how much money the customer has spent. If the value is over 5000, the method returns "gold", if over 1000, "silver", and if 1000 or below, "basic".

The output should be like this↓

Normal Natalia
Programmer Pete
basic
Coder Cara
silver
Goldson Gilbert
gold

Here is the prepared code which is not allowed to be edited.

function Person(firstname, lastname) {
  this.firstname = firstname;
  this.lastname = lastname;
}
Person.prototype.name = function() {return (this.lastname + " " + this.firstname);}

Here is what I have done so far.

function Customer(firstname, lastname,name,id, spent) {
    Person.call(this, firstname, lastname, name);
    
    this.id = id;
    this.spent = spent;
    
    this.bonus = function() { /* check how much the customer has spent */
        if ( this.spent >= 5000 ) {
            return "gold";
        } else if ( this.spent >= 1000 && this.spent < 5000 ) {
            return "silver";
        } else {
            return "basic";
        }
    };
}

Here is also the prepared code.

var person = new Person("Natalia", "Normal");
var customer1 = new Customer("Pete", "Programmer", 1);
var customer2 = new Customer("Cara", "Coder", 2, 1500);
var customer3 = new Customer("Gilbert", "Goldson", 3, 8000);

console.log(person.name());
console.log(customer1.firstname());
console.log(customer1.name());
console.log(customer1.bonus());
console.log(customer2.name());
console.log(customer2.bonus());
console.log(customer3.name());
console.log(customer3.bonus());
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda