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

170
Visualizações
Why is the call() function not working with an object created with an already created class

I am a javaScript beginner. I was trying to run a call function to an object which I created using a class. I expected an output - "Virat Kohli" with the following code :

    class person {
constructor(firstName,lastName){
    firstName = this.firstName;
    lastName = this.lastName
}
}


perFullName = {
    fullName : function(){
        return this.firstName + " " + this.lastName;
    }
}

const vk = new person("Virat","Kohli");
console.log(perFullName.fullName.call(vk))

But this returned me undefined undefined in my VS code console, but when I replaced it and created the object like this :

const vk = {
    firstName : "Virat",
    lastName : "Kohli"
}

It returned the expected output.

Can you please tell me the reason and how I can fix it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The variable assignments in the constructor are back-to-front, they should assign the input values to the member variables.

Your fullName function can be moved into the class as a read only property getter:

class person {
    constructor(firstName, lastName){
        this.firstName = firstName;
        this.lastName = lastName
    }
    
    get fullName() {
        return this.firstName + " " + this.lastName;
    }
}

const vk = new person("Virat", "Kohli");
console.log(vk.fullName);

about 4 years ago · Juan Pablo Isaza Relatório
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