Back
Wrapping things in JS
This helper memo outlines how to approach encapsulating content in Javascript.
Quick Rules
- Use a module with top-level variables and functions to access them when you want singleton behavior and there is no need for instances.
- Use a module with top-level execution when code should run immediately and other parts of the app will never be affected by it.
- Use a class (i.e. function) when you need full control and multiple instances.
- Use a singleton class when you need full control and one instance.
- Use a function for reusable, stateless behavior that doesn't need an instance.