Reputation:
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.
Upvotes: 0
Views: 360
Reputation: 16313
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.
Upvotes: 0
Reputation: 189437
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.
Upvotes: 1
Reputation: 53356
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.
Upvotes: 0
Reputation: 11988
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.
Upvotes: 1
Reputation: 321578
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...
Upvotes: 1