When is it a mistake to force a 'classic' design pattern into idiomatic JavaScript?
Short answer
Many GoF patterns exist to work around limitations of statically-typed, class-heavy languages.
Advertisement
AModel answer
Many GoF patterns exist to work around limitations of statically-typed, class-heavy languages. JavaScript's first-class functions and closures often solve the same problem more simply — e.g., a full class-based Strategy pattern is often overkill when a plain object of functions or even a single higher-order function would do.
// Overkill class-based Strategy:
class AddStrategy { execute(a,b) { return a+b; } }
// Idiomatic JS equivalent:
const add = (a, b) => a + b;Real-world example
Recognizing when a Java-style pattern is being copy-pasted into JS unnecessarily, adding ceremony without benefit.
Likely follow-ups
- What JavaScript language features most commonly replace the need for classic OOP patterns?
Related topics