DEV Community

Discussion on: Design Patterns - Strategy Pattern in JavaScript

Collapse
 
suchcodemuchwow profile image
Meme Oriented Programmer

Pretty tiring, boring, unnecessary fancy words for js world.

const strategyA = {
  run : function () {} 
}
...

const strategies = {
 strategyA,
 strategyB,
 ...
}

// below string should be a variable to make it more dynamic. 
// And can be wrapped with try/catch if not exist will throw error
strategies['strategyA'].run()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
carlillo profile image
Carlos Caballero

Thanks! 🙃

Collapse
 
noleynarbar profile image
Nolan Geiger

As a former academic, in another field, this article reminded me of when I couldn't explain something to a 12-year-old. I'm new to design-patterns, and have avoided them, but form what I understand, batuxd, the strategy-pattern is simply when one should encapsulate (or store and make accessible for later use) what varies.