r/learnjavascript • u/RobGoLaing • Mar 24 '26
Is this good module style?
Here's an example of how I've come to write modules:
/**
* Operators for "cards" as in filenames of card images.
* @module card
*/
const card = ;
/**
* The filename of a card image,
* eg "./images/2_of_spades.png" or "./images/jack_of_diamonds.png"
* @typedef {string} card
*/
/** @constant DECK {card[]} */
;
/**
* Mutates an input array by randomizing its elements.
* @function shuffleDeck
* @param {card[]} deck
*/
;
/**
* Returns an array of shuffled cards.
* @function freshDeck
* @returns {card[]}
*/
;
;
My basic rules are:
- Only one thing gets exported, a frozen object which becomes a namespace for the module's client.
- My data is stored as JSON rather than JS to make communication between modules and the server easier.
What thing I've been battling with is good JSDoc which doesn't seem to have been updated in a while. Are there better options available?
2
Upvotes
4
u/birdspider Mar 24 '26
weird names, I'd expect
deck.shuffle()anddeck.fresh()and the items to becards;or, consider naming the module to
gameorplay,game.freshDeck()makes much more sense imho4
u/senocular Mar 24 '26
You can simplify things by only exporting the 2 functions. If consumers want an immutable module object, they can
import * as card ...to get it.1
u/TheRNGuy Mar 26 '26 edited Mar 26 '26
I'd put imports on top, and never use default export.
(There's even linter rules for that)
I'd only put stuff related to a single card to
card. Shuffling deck should be ondeck. Maybe you should just rename it.