1

A Web frontend javascript application, in production, will have little to nothing "exposed", that is, declared as a global var or attached to window.

  • all code minimized and appended to one (or a few) js file(s);
  • little to nothing "exposed", that is, declared as a global var or attached to window.

During development, though, life is easier if the code is readable, split in several files, and also if prototypes and relevant instances are accessible from console.

To be more clear (leaving aside minimization, which is easy obtained with a number of different tools), in production I'll have something like:

(function() {
  var Greeter = function() {
  };

  Greeter.prototype.msg = function() {
    return 'Hello, world!';
  };

  Greeter.prototype.greet = function() {
    console.log(this.msg());
  };

  new Greeter().greet();
}());

This way, my code will do its stuff without exposing anything: neither the Greeter object nor its instances are accessible to other code.

(Of course this is just one of many ways to accomplish this, but that's not the point of the question).

Debugging this code, though, is hard, and unit testing is impossible.

In order to allow both debugging and testing, I would usually attach both Greeter and its instance to the window object, or to some other object.

So, during development, I'll use something like:

(function() {
  var Greeter = function() {
  };

  Greeter.prototype.msg = function() {
    return 'Hello, world!';
  };

  Greeter.prototype.greet = function() {
    console.log(this.msg());
  };

  window.Greeter = Greeter;
  window.greeter = new Greeter();

  window.greeter.greet();
}());

This way I'll be able to unit test Greeter, and also to interrogate it from the browser's console to check its status.

Is there a tool, or a set of tools, or some different way to organize my code, so that it's possible to pass from the development version to the production one (which will also be minimized)?

giorgian
  • 3,795
  • 1
  • 30
  • 48
  • I am also looking for a similar automation process. I am currently using a neat ANT build script provided with the HTML5 boilerplate (see it in action http://www.youtube.com/watch?v=OXpCB3U_4Ig). I am also taking a look at Grunt.js, as referenced in the first answer here below, but currently every tutorial around seems to reference the old version of it. Still, you might want to give it a go. this seems to be a nice solution for you, as it can be customized according to your needs. – Aurelio Mar 15 '13 at 13:52

1 Answers1

1

There is no single package or executable you can install that will get you 100% of the way there. You will need to combine an editor, command line tools and your browser to create an effective web application / javascript development environment.

3.18.13: Added a link for Sublime Web Inspector. Debug Javascript inside of Sublime Text! http://sokolovstas.github.com/SublimeWebInspector/

Editor

Things to look for: Plugin system, system highlighting, linting, autocomplete. If you are using an editor today that supports plugins your best bet is to stick with it and setup linting and syntax highlighting. If you are looking for some recommendations all of the following are solid choices.

  • Sublime Text 2 (free trial)
  • Textmate (commercial, 30 day trial)
  • VIM (free)
  • Webstorm (commercial, 30 day trial)

Workflow Tools:

I recommend starting with a high level toolset like Yeoman or lineman. They are somewhat opinionated but provide a complete workflow and will allow you to get stuff done quickly. Once you are comfortable with using it you can peek under the covers and pick and customize it to your needs.

  • Yeoman : Provides scaffolding, package management, dev server, concatenate & minify and will run specs

  • Lineman: Dev server, concatenate & minify, run specs

  • Grunt: More low level (used by both Yeoman and Lineman). Similar to ruby's rake

  • VCS: Not to be overlooked, a good command line based VCS is essential. I recommend Git, again use what you are comfortable with to start.

Browser:

The development tools in the browser will provide you with a console, and debugging tool. Spend some time researching and really getting to know how to use the development tools provided in the browser. They are extremely powerful.

  • Webkit browser (Chrome or Safari): Built in Developer Tools (Command option J).

  • Firefox + firebug

  • Browser testing: Highly recommend browserstack for cross browser testing.

Scott Puleo
  • 3,684
  • 24
  • 23
  • Well, I didn't mention editor, git and browser because I thought they were obvious... – giorgian Mar 15 '13 at 12:33
  • BTW Sublime Text is no freeware. Its evaluation period does not expire but you have to pay for a licence if you keep using it. – Aurelio Mar 15 '13 at 13:44
  • Sorry giorgian, I did not mean to muddy the waters with tools you are already familiar with. When I made the switch from server side to client side development I found the transition from working primarily in an IDE with dialogs and menu's to a simple text editor with command line tools a bit jaring. I was just sharing what I wish someone told me when I made the transition. – Scott Puleo Mar 15 '13 at 14:30