views:

192

answers:

5

I have three javascript files that I want to merge into a single file. Is it possible to just copy paste all of the code into one file or will there be namespace conflicts and other problems?

EDIT: I'm worried that each file acts like a namespace encapsulating each file's code, and that this encapsulating will cease to existing if I merge the files.

+1  A: 

If the script files were all loaded in the <head> and you paste them in the same order they appeared in the HTML then there shouldn't be any problems.

Having said that, if they use document.write I'm not sure...

Greg
I'm not sure- if one of the javascript files was busted, I think it would be ignored individually if included individually. If you merge it then the merge result will be busted. Just need to have some tests that make sure some of the script loaded/works to address this.
Frank Schwieterman
+1  A: 

If they all work when loaded sequentially, it makes no difference if you concatenate them to a single file and you that instead. Just make sure you put them together in the same order as when you load the seperately.

Guðmundur H
A: 

Try it and see if it works. :) The namespace conflicts will depend solely on the code, so without posting it, it will be hard to tell you. You shouldn't have problems putting them in one file so long as they don't have errors already, but if one is dependent on another, make sure you put them in the proper order.

VirtuosiMedia
+1  A: 

In answer to your edit: No, each file will not act as a separate namespace.

The top level in each file will share the same global namespace. Hence the having single file with the contents of all three is the same as referencing each seperately assuming the content appears in the same order.

AnthonyWJones
A: 

There's absolutely no namespace encapsulation in JavaScript, unless you go out of your way to make it happen. By default, everything ends up in the global namespace. The JavaScript module pattern helps reduce global namespace pollution.

George V. Reilly