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

184
Visualizações
Javascript Objects, Constructors, only returning final value?

I'm learning about objects and constructors so created an object for a book.

It works fine after some tests with one element (book OR author OR pages OR read) but when I add more than 1 it only returns the final value.

For example, the below is only returning "nope" when I want it to return the title, author, pages, read (as I thought my function "bookInfo" declares?

What am I missing here as I've checked my code against some examples and can't see any issues?

function Book(title, author, pages, read) {
    this.title = title
    this.author = author
    this.pages = pages
    this.read = read
    this.bookInfo = function() {
        return (title, author, pages, read)
    }

}

const LOTR = new Book('lord of the rings', 'JRR', '366', 'nope');

console.log(LOTR.bookInfo());

// should print 'lord of the rings', 'JRR', '366', 'nope') but only returns 'nope' (or whatever the last value is?)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can return the assigned values in an array:

this.bookInfo = function() { 
  return [this.title, this.author, this.pages, this.read]; 
}

or object:

this.bookInfo = function() { 
  return {title:this.title, author:this.author, pages:this.pages, read:this.read}; 
}

or string:

this.bookInfo = function() { 
  return `${this.title} ${this.author} ${this.pages} ${this.read}`; 
}
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