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

123
Visualizações
Object Method returns the code as a string

I've just started to learn javascript and can't seem to call an object method properly. I've even copy pasted the code from here to try to understand my mistake but it just returns the code as a string. My code:

function greetings(name){
    document.getElementById('someText').innerHTML = 'Hello' + ' ' + name;
}

const person = {
    firstName: "John",
    lastName: "Doe",
    id: 5566,
    fullName: function() {
      return this.firstName + " " + this.lastName;
    }
  };
  

  greetings(person.fullName);
<div id="someText"></div>

And the result:

Hello function() { return this.firstName + " " + this.lastName; }

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

0

You have to call the method, which will return a string of the person's name, and then that will be passed to the greetings function.
Without the () the method is not executed and the method itself is passed to the greetings function.
Since it is treated as a string in the function you get the toString() method being called on the function which results in the function code itself.

function greetings(name){
    document.getElementById('someText').innerHTML = 'Hello' + ' ' + name;
}

const person = {
    firstName: "John",
    lastName: "Doe",
    id: 5566,
    fullName: function() {
      return this.firstName + " " + this.lastName;
    }
  };
  

  greetings(person.fullName());
  console.log(person.fullName.toString());
<div id="someText"></div>

about 4 years ago · Juan Pablo Isaza Relatório

0

you can solve your problem in two ways.

//firstly

 greetings(person.fullName()); // calling the function when you pass it as a Arguments. 

// secondly
function greetings(name){
    document.getElementById('someText').innerHTML = 'Hello' + ' ' + name();
// if you always except a function as a peramiter just call it when you use it.
}

you define fullName as a person object property which is a function. For getting the proper value you have to call the function. we used () after the function name for calling it. if the function didn't call it returns the function body.

about 4 years ago · Juan Pablo Isaza Relatório

0

since you defined fullname as a function here:

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

then you have to call it, like: greetings(person.fullName()); with the additional brackets at the end

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