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

148
Visualizações
Most common way to extend an object

Let's say I have the following object to represent a book:

const book = {
    name: undefined,
    author: {
        surname: undefined
    }
}

What would be the suggested way to create new instances/objects of the book (aside from using the class approach)? I suppose the ways I've seen it done are as follows:

const book = {
    name: undefined,
    author: {
        surname: undefined
    }
}
let hamlet1 = Object.assign(book, {name: "Hamlet", author: {surname: "Shakespeare"}});
let hamlet2 = {...book, name: "Hamlet", author: {surname: "Shakespeare"}}
let hamlet3 = Object.create(book);
hamlet3.name = "Hamlet";
hamlet3.author.surname = "Shakespeare";
console.log(hamlet1, hamlet2, hamlet3);

Is there a suggested way to create instances of a typed object? I suppose the equivalent of having a struct in C and then filling its values after declaration.

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

0

All three behave differently

Take a look at how each individual method effects BOOK

First method ... book and hamlet are the same actual object

const book = {name: undefined, author: {surname: undefined}};
let hamlet = Object.assign(book, {
  name: "Hamlet",
  author: {
    surname: "Shakespeare"
  }
});
console.log('book', book, '\nhamlet', hamlet);
console.log(`book is hamlet`, book === hamlet);
console.log(`book.author is hamlet.author`, book.author === hamlet.author);
.as-console-wrapper {max-height: 100%!important; top:0; }
.as-console-row::after { display:none !important; }

Here, book and hemlet share no common objects

const book = {name: undefined, author: {surname: undefined}};
let hamlet = { ...book,
  name: "Hamlet",
  author: {
    surname: "Shakespeare"
  }
};
console.log('book', book, '\nhamlet', hamlet);
console.log(`book is hamlet`, book === hamlet);
console.log(`book.author is hamlet.author`, book.author === hamlet.author);
.as-console-wrapper {max-height: 100%!important; top:0; }
.as-console-row::after { display:none !important; }

Here, book.author === hamlet.author - so they too are the same object

const book = {name: undefined, author: {surname: undefined}};
let hamlet = Object.create(book);
hamlet.name = "Hamlet";
hamlet.author.surname = "Shakespeare";
console.log('book', book, '\nhamlet', hamlet);
console.log(`book is hamlet`, book === hamlet);
console.log(`book.author is hamlet.author`, book.author === hamlet.author);
.as-console-wrapper {max-height: 100%!important; top:0; }
.as-console-row::after { display:none !important; }

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