I looked through all the other (excellent) answers to SO (especially this: How does JavaScript close 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 open access.
Another that I think of has it as a factory generator:
<script> function carFactory( make ) { var m = make; return { manufacture: function ( model ) {console.log("A " + m + " " + model + " has been created");} } } toyotaFactory = carFactory("toyota"); hondaFactory = carFactory("honda"); toyotaFactory.manufacture("corolla"); toyotaFactory.manufacture("corolla"); hondaFactory.manufacture("civic"); </script>
It is output:
A toyota corolla has been create A toyota corolla has been created A honda civic has been created
What do you think is its acceptable precedent for closing (i.e. the creation of several plants using the same code base)? Or can I achieve the same using something much better?
Please note that the question of technical implementation of closures and acceptable examples of use in design / application development is less.
Thanks.
javascript closures application-design
Software guy
source share