Javascript multiple files best practices
I've been branching out from the freecodecamp curriculum and looking at random games that people have posted online (github, codesandbox.io, etc) and have seen various ways that people link multiple .js files together and thought about what the preferred way this is done in the industry so I don't pick up any bad habits. Show
Some people break down all the various functions they need in multiple files and then link them on the HTML file with something like , which seems to be the easiest though not the neatest method and without discipline, it's easy to make them too long. I also found out doesn't work on codesandbox.io and results in a bunch of errors because it can't find what was declared on the other files and requires using imports and exports. Other people I've seen take the approach of having just one main index.js file that goes in the HTML and the index.js file will then import all the other javascript files it needs. On bigger projects I see that index.js only imports a main.js which then imports all the various .js files (which have just a single class in them) that are used in the project. So which style is preferred and I'm sure I'm leaving out other ways people do things so please let me know how you or your company do things and why. Just from my view, it seems having just one point of entry makes things a lot neater and lets you reuse the classes later for other things too. Photo by Rodion Kutsaev on UnsplashJavaScript is an easy to learn programming language. It’s easy to write programs that run and does something. However, it’s hard to account for all the uses cases and write robust JavaScript code. In this article, we’ll look at how to name our files and proper encoding for our files. Also, we look at the right way to deal with modules. File NamesFile names should be all lower case and may include underscores or dashes. We shouldn’t put any additional punctuation in our file names. The file names should end in
For instance, File EncodingThe encoding of the file should be UTF-8. This is standard across all platforms so that it won’t cause issues when we run them anywhere. White Space CharactersThe ASCII horizontal whitespace character should be the only one in source files. Other whitespace characters should be escaped. Tabs shouldn’t be used for indentation since they may be interpreted differently in different platforms. However, tabs can be converted to spaces automatically. Special Escape SequencesSpecial escape sequences should have a corresponding numeric escape. So Non-ASCII CharactersWe should use Unicode equivalent for non-ASCII characters in our code files. Source File StructureWe may want to enforce some structure in our source files. For instance, we may have JSDoc comments for documentation. We may put them in folders so that there’s a clear hierarchy in our project structure. ES ModulesModules are used in most new JavaScript projects now. Therefore, we should be mindful of some rules regarding modules. Import LengthLength of This way, no one has to scroll horizontally to read the whole line. Import PathsWe should use the For instance, we can write: import './foo'; or: import * as foo from './foo.js'; or: import { name } from './foo.js'; We don’t need the extension when importing files. Importing the Same File Multiple TimesWe shouldn’t import multiple members of the same file in different For instance, instead of writing: import { bar } from './foo'; We write: import { bar, baz } from './foo'; Naming ImportsWe can name imports with the The name should be camelCase. For instance, we write: import * as fooBar from './fooBar'; Naming Default ImportsWe can name default imports with camelCase also. For instance, we can write: import fooBar from './fooBar'; Naming Named ImportsAlso, we can change the name of named imports so that we can use the name that we want to use. For instance, we can write: import { Cat as FatCat } from './animal'; for constructor imports or: import { cat as fatCat } from './animal'; for other imports. Named vs Default ExportsWe can have named and default exports just like imports. For instance, we can create a default export by writing: export class Foo { ... } and a named export by writing: export { Foo } Export Container Classes and ObjectsWe shouldn’t export container classes and objects Therefore, instead of writing the following: export class Container { We write: export function foo() { Mutability of ExportsDon’t export variables that are mutable. This means
anything declared with For instance, instead of writing: export let foo = 1; We write: export const foo = 1; export from StatementsWe should wrap So we write: export * from './foo'; or: export { bar } from './another.js'; Photo by
Shyam on UnsplashCircular Dependencies in ES modulesWe shouldn't create circular dependencies between ES6 modules, directly or indirectly. For instance, we shouldn’t write:
import './a'; and:
import './b'; ConclusionWe should be careful when working with file names and modules. File names should be named in lowercase with underscore or dashes, We should use modules and they shouldn’t have circular dependencies. Code files should be UTF-8 encoded to avoid issues running them. Is it good to have multiple JS files?You can write your JS in separate files, but when it comes to deploying, it's more efficient to minify them all into a single file. For each script you load in your browser, you make a round-trip to the server, so it makes sense to minimize those.
How do I include multiple JavaScript files?Method 1: Use JavaScript to include another JavaScript file. Add the following function in your web page. function loadScript(url) { var head = document. getElementsByTagName('head')[0]; var script = document. ... . just call the below loadScript function, where you want to include the js file. loadScript('/js/jquery-1.7. min.. Should all my JavaScript be in one file?To avoid multiple server requests, group your JavaScript files into one. Whatever you use for performance, try to minify JavaScript to improve the load time of the web page. If you are using single page application, then group all the scripts in a single file.
How do you organize JavaScript files?Let's take a closer look at the 5 ways to organize your JavaScript the right way.. Comment Your Code. ... . Use ES6 Classes. ... . Use Promises in Your Javascript Data Structures. ... . Keep Things Separated. ... . Use Constants and Enums.. |