Home > Mobile >  Most common way to extend an object
Most common way to extend an object

Time:03-18

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.

CodePudding user response:

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; }

CodePudding user response:

It may be simpler to create a function and check the types of the parameters. This also gives control over the error that is thrown.

function Book(name, surname) {
  if (typeof(name) === 'string' && 
      typeof(surname) === 'string') {
    this.name = name;
    this.surname = surname;
  }
  else {
    err="Incorrect type \n"
    for(i=0;i<arguments.length;i  ) {
      err  = arguments[i]   ": "   typeof(arguments[i])   "\n";
    }
    throw new Error(err);
  }
}

b1 = new Book("Tom Sawyer", 1)
  • Related