当前位置: 动力学知识库 > 问答 > 编程问答 >

When should I save Javascript code into different files or the same file?

问题描述:

I am a beginner of Javascript. I have done some Python and Java before.

I am not clear when and why we should store codes into different .js files or the same file.

Is there any conventions and rules for this?

网友答案:

Well, the main reason of javascript code into different folders is for organization. Similar to Java or Python, or any language, you should organize your code in different files to get a code that any other in your team can read easily.

But in production scope, or better said, in your website, it's a best practice to reduce the number os javascript files for performance reasons. If you have 2 javascript files, the server will send 2 request. In the case that you have 10 or 15 files, the web will have performance problems.

So, in development it's important to have multiple files to organice your code, but in production you can minify then into one. Uglify is a good tool to monify your code into one file.

Hope it helps.

分享给朋友:
您可能感兴趣的文章:
随机阅读: