KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have looked through all the other (excellent) answers on SO (especially this: How do JavaScript closures work? ) but I wanted your feedback on my understanding of the concept. I understand that one use case is to hide the implementation of private methods from public access. The other one that I think of is having it as a factory generator: <script> function carFactory( make ) { return { manufacture: function ( model ) { console.log("A " + make + " " + model + " has been created"); } } } toyotaFactory = carFactory("toyota"); hondaFactory = carFactory("honda"); toyotaFactory.manufacture("corolla"); toyotaFactory.manufacture("corolla"); hondaFactory.manufacture("civic"); </script> This outputs: A toyota corolla has been create A toyota corolla has been created A honda civic has been created So do you think its a valid use case for closures (i.e. creating multiple factories using the same code base)? Or can I achieve the same thing using something much better? Please note that the question is less about the technical implementation of closures and more about valid use cases in application design / development. Thanks.
Tags (comma-separated)
Save Edits
Cancel