DEV Community

Cover image for Structs in JavaScript
JoelBonetR
JoelBonetR

Posted on

Structs in JavaScript

What is a struct?

A struct is used for building data structures. These structures are used to group related data together.

There are differences on how they work behind the scenes depending on the programming language but the fun thing is that there are no structs in JavaScript.

When you want to create a custom data structure in JS you usually do a single thing. Creating an Object.

const user = {
  id: 0,
  name: '',
  country: '',
  pets: [],
}
Enter fullscreen mode Exit fullscreen mode

But is there any other way to do that?

Let's try to create a function to dynamically generate "structs" in JavaScript!

dope javascript structs

/**
 * @constructor Generates a constructor for a given data structure
 * @param {string} keys separated by a comma + whitespace. struct('id, name, age')
 * @returns {constructor} Constructor for the new struct
 */
function struct(keys) {
  const k = keys.split(', ');
  const count = k.length;

  /** @constructor */
  function constructor() {
    for (let i = 0; i < count; i++) {
      this[k[i]] = arguments[i];
    }
  }
  return constructor;
}
Enter fullscreen mode Exit fullscreen mode

Usage:

const user = struct("id, name, country");
const johnny = new user(1, 'John', 'UK');
console.log(johnny.name); // john
Enter fullscreen mode Exit fullscreen mode

We can also assign complex structures at our properties!

const user = struct("id, name, country, pets");
const pepe = new user(1, 'Pepe', 'ES', ['Baxter', 'Flurfrils']);

console.log(pepe.pets); // ['Baxter', 'Flurfrils']
Enter fullscreen mode Exit fullscreen mode

Or adding a struct inside the other:

/**
 * @typedef UserInfo
 * @property {string} phone
 * @property {number} age
 * @property {string} hairColor
 * @constructor
 */

/** @type {ObjectConstructor|any} */
const UserInfo = new struct('phone, age, hairColor');

/** @type {UserInfo} */
const extraInfo = new UserInfo('555-777-888', 31, 'blonde');


/**
 * @typedef User
 * @property {string} phone
 * @property {number} age
 * @property {string} hairColor
 * @constructor
 */

/** @type {ObjectConstructor|any} */
const User = new struct('id, name, country, info');
/** @type {User} */
const John = new User(1, 'John', 'US', extraInfo);
Enter fullscreen mode Exit fullscreen mode

Now we can do:

John.country // 'US'
John.info.phone // '555-777-888'
Enter fullscreen mode Exit fullscreen mode

Okay okay it's creating an object at the end but JavaScript is an object-based language just like Python and others, so in truth everything is an object.

And you can't say that this isn't funny!


Disclaimer: This code was meant to instruct on how you can play with Objects in JavaScript and may not be suitable for a production build. Not because it won't work but because you can make code that is hard to maintain. Please, don't forget to create typedefs (either you use TS or JSDoc with TS Pragma) and always remember to document your code properly.

Discussion (0)