What are some better ways to simulate a `javascript struct`?

Building on Emma’s point, I’ve seen cases where using just objects is fine - but if you want more structure or plan to attach methods, I recommend using ES6 classes for your javascript struct.

Here’s how you can upgrade that:

class Item {
  constructor(id, speaker, country) {
    this.id = id;
    this.speaker = speaker;
    this.country = country;
  }

  describe() {
    return `${this.speaker} from ${this.country}`;
  }
}

const myItems = [
  new Item(1, 'john', 'au'),
  new Item(2, 'mary', 'us')
];

Why does this help? :white_check_mark: Encapsulation – You can wrap logic inside the class, making the data and behavior live together. :white_check_mark: Structure – Makes it clear what shape each javascript struct instance should have. :white_check_mark: Extendability – You can subclass or extend later if your app grows.

If you think your code will evolve beyond simple data holding, classes set you up nicely.