Functions, objects, and modules: looser shapes than Python classes and modules
Map Python functions, classes, and modules onto JavaScript's function-first and object-first patterns.
by the end of this lesson you can
- →Chooses functions, objects, or classes intentionally
- →Keeps module boundaries clear
- →Avoids preserving Python structure when a simpler JavaScript shape is enough
Overview
Python gives you modules, classes, and dictionaries that usually keep their responsibilities fairly legible. JavaScript and Node let you organize code in many ways, so the challenge is choosing a structure that stays clear instead of translating Python habits literally.
In Python, you often
use modules for namespacing, classes for grouped behavior, and dictionaries for lightweight structured data.
In JavaScript/Node, the common pattern is
to choose among functions, plain objects, and classes more selectively, often keeping modules and object literals lighter than their Python equivalents.
why this difference matters
This is where JavaScript's flexibility can help or hurt. Clear boundaries matter more because the language allows many shapes that all technically work.
Python
class User:
def __init__(self, name):
self.name = name
def greet(self):
return f"hello {self.name}"JavaScript/Node
class User {
constructor(name) {
this.name = name;
}
greet() {
return `hello ${this.name}`;
}
}Deeper comparison
Python version
def load_user(repo, user_id):
return repo.get(user_id)JavaScript/Node version
function loadUser(repo, userId) {
return repo.get(userId);
}Reflect
When is a plain object or function enough in JavaScript, and when does a class actually help?
what a strong answer notices
A strong answer mentions choosing the lightest structure that still communicates responsibility clearly.
Rewrite
Rewrite this small Python class/module pattern into a JavaScript or Node shape that is no heavier than necessary.
Rewrite this Python
class Cache:
def get(self, key):
...
def load_user(cache, user_id):
return cache.get(user_id)what good looks like
- Chooses functions, objects, or classes intentionally
- Keeps module boundaries clear
- Avoids preserving Python structure when a simpler JavaScript shape is enough
Practice
Design a Node module that loads and formats a user profile, starting from a Python mental model but landing in idiomatic JavaScript.
success criteria
- Separates module responsibilities clearly
- Uses object or class structure only where it helps readability
- Keeps the export shape easy to understand from the caller side
Common mistakes
- Porting Python classes one-to-one when a function or plain object would be clearer.
- Using objects as unstructured dumping grounds.
- Treating module exports as an afterthought instead of part of the interface design.
takeaways
- ●This is where JavaScript's flexibility can help or hurt. Clear boundaries matter more because the language allows many shapes that all technically work.
- ●A strong answer mentions choosing the lightest structure that still communicates responsibility clearly.
- ●Separates module responsibilities clearly