-1

For a new page I'm working on, I want to make sure I do everything “right” to the best of my ability. I was wondering what the best practices were re. one JS file per page or one file containing everything. I found this question which helped some, but raised more questions.

I pretty much only use JS for three things:

  1. transitioning things on button clicks (showing/hiding panels, etc.),
  2. pre-validating forms, and
  3. AJAX calls.

When I compare my use cases to the namespacing approach, it seems like overkill; I don't really understand why I would need to set up such a complex framework to work with JavaScript. This leaves me with two questions:

  1. For what I'm doing, should I use one JS file per page, or use Irish's namespace technique and a single script import?
  2. What the hell are people using JS for that requires so much structure?
Community
  • 1
  • 1
Danthier
  • 57
  • 6
  • What is the Irish's namespace technique? – Paco Sep 22 '13 at 20:01
  • Please include a short, reproducible code example to show the problem. ShackOverflow has difficulty solving code issues without code in the question. ;) – Joe Frambach Sep 22 '13 at 20:03
  • 1
    Paco: I included a link, but here's another directly to his article: http://www.paulirish.com/2009/markup-based-unobtrusive-comprehensive-dom-ready-execution/ Joe: I guess this was more a philosophical question than a direct code question. – Danthier Sep 22 '13 at 20:08
  • @Danthier: As he mentions himself in that article, this technique is called *routing*. [tag:Namespaces] are a different thing. You should always use them for your modules. You should not use DOM-based routing if you don't feel you need it (i.e. it would not simplify things for you). – Bergi Sep 22 '13 at 20:53

2 Answers2

1

If your pages don't have anything in common, you might use a script file for each page. If you've got a lot of logic common between your pages, you'd probably want to put those common bits into a file of its own and include it wherever you need it.

As for why so much structure is necessary, people are making more and more complex things with JavaScript. Consider Gmail, for example. I'd imagine there's quite a bit of code in there, and without much structure, it would become difficult to maintain quickly.

icktoofay
  • 122,243
  • 18
  • 242
  • 228
  • I guess that makes sense, I was just thinking that my files would each individually would be very small, so it might make sense to make one request and cache it. Any advice for what the "biggest" my file should be before I should start splitting? – Danthier Sep 22 '13 at 20:10
  • @Danthier: I wouldn't think about splitting until it was at least 10 KiB. Even then, it's not critical to split it. If it hits 75 KiB, then I might start splitting it up. – icktoofay Sep 22 '13 at 20:16
1

OK, That page is from 2009 - The way Javascript is used on the web has changed a lot since then.

Now that most web pages contain multiple third-party Javascript files from different sources (and different developers). It makes a lot of sense to encapsulate your code in a custom namespace to prevent your code being overridden by other code using the same variable names, and it isn't any harder than:

Mynamespace= {};
Mynamespace.foo = "bar";
Mynamespace.foobar = function(){
    //function body
};

Writing structured Javascript isn't about adding complexity. Writing structured Javascript allows you to encapsulate behaviours and responsibilities into re-useable portions of code that are much easier to test, maintain, re-use and extend.

You don't even need to make the single-file/multiple files judgement. You can use a framework like require.js that compiles all of your separate code files into one single file for deployment.

JavaScript is now officially a first-class language in Visual Studio Its being used to write web-servers, templating systems and even 3d engines.

Welcome to 2013 ;-)

_Pez

underscorePez
  • 897
  • 8
  • 19