I know this is an old query, but I wonder why no one mentioned complex objects. In my opinion, this is the easiest way to organize and store your data.
- create your constructor with ES6 destructuring:
function Book({reference, publication, author}) { this.reference = reference; this.publication = publication; this.author = author; }
- Implementation:
var rc = new Book({ reference: { title: 'Robinson Crusoe', pages: 342, chapters: 16, protagonistFavoriteColor: 'lilac' }, publication: { publisher: 'John Smith co.', date: '04-25-1719' }, author: { name: 'Daniel Defoe', homeTown: 'London' } });
- Using:
console.log(rc.reference.title); // 'Robinson Crusoe' console.log(rc.publication.date); // '04-25-1719' console.log(rc.author.name); // 'Daniel Defoe'
This method also gives you the opportunity to access the entire category.
console.log(rc.author); // {name: "Daniel Defoe", homeTown: "London"} console.log(rc.reference); // {title: "Robinson Crusoe", pages: 342, chapters: 16, protagonistFavoriteColor: "lilac"}
When you are familiar with this organization, creating objects becomes simple and fun.
source share