Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

At the moment every javasrcipt file in our project includes closure wrapper. Before deploying all files are concatenated and minimized.

And i'm asking myself, do we really need all that closures OR one closure added after concatenation will do the job.

I can see only one reason not to do that - the variable we are sharing between files will get in the global namespace. But there is no conflict so is don't see any problem.

The real question is what other cons may be? Or more generally what approach is better - one closure or many?

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

Yes, you need all the closures so the files are separate from each other.

If you concatenate more then one file inside the same function then they will share variables, just as if they had been global. The only advantage at this point would be insulating your code from external 3rd party Javascript.


In any case, I would say that the best thing you can probably do is use a Javascript modularization tool like RequireJS so you can stop worrying about concatenating and generating the files yourself.

share|improve this answer
    
Ok. The best approach is too use modularization tool. –  sdespolit Mar 26 '12 at 16:25
add comment

But there is no conflict so is don't see any problem

thats a horrible understatement, wrap every single file in a closure so that the you can "file local" variables inside that file.

The fact that wrapping it in a closure adds 15 bytes is the only disadvantage, if you care about those 15 bytes then your micro optimizing.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.