Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

146
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda