Over a million developers have joined DZone.

New open source project: Strongly-Typed Javascript (STJS).

DZone's Guide to

New open source project: Strongly-Typed Javascript (STJS).

· ·
Free Resource

The subject is not new (GWT is there for a while already). Strongly –typed languages seem to keep being interesting in a developers’ world very attracted currently by dynamic, weakly typed languages. Dart is also another example. We believe that for bigger Javascript projects (let’s say more than 2000 lines of code), the Javascript becomes very hard to maintain, almost impossible to refactor. And even though the IDEs support for Javascript is increasing, it’s still way behind the one for Java. Try renaming a function in a Javascript project (one that has the same name as other function that you don’t want to rename)!

GWT seemed to be the ideal solution, but as with any development project, it’s very hard to satisfy everybody!  We don’t believe in big frameworks, all-in-one approach. We prefer smaller library focused on a single thing, that integrates nicely with other libraries that excel in their domain.
Our open source project “Strongly-typed Javascript” (STJS) focuses only on that part – generate the Javascript code from a Java code. We took Java as the input language as our server-side is written also in Java. So it becomes easier to share objects between client and server (via AJAX for example).

The project is provided as a Maven plugin that is executed after the Java sources of your project are compiled. Using the Maven-Eclipse integration you can also have the Javascript generated right after the input Java source file is saved (no need to re-build the project). This makes the developing roundtrip very short and it allows you to see the changes with a simple refresh of the page.
Our main concern when we built the library was to stay as close as possible to the initial code. Very few things are modified between the Java source and its Javascript counterpart. When debugging in a browser it’s cool to see a meaningful code! Here is a short example:

form.find(".ok").click(new EventHandler() {
  public boolean onEvent(Event evt, Element THIS) {
    $(that.row(stockData)).appendTo("table tbody");
    return true;

form.find(".ok").click(function(evt) {
  $(that.row(stockData)).appendTo("table tbody");
  return true;

As you could figure out from the example, we don’t propose our GUI components - JQuery and the community around it do already a great job. We only built a bridge that allows you to use JQuery and JQuery UI plugins directly in the Java code.

If you’re interested in our project, feel free to have a look on the project’s web site http://st-js.sourceforge.net, give it a try and don’t hesitate to send us your feedback.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}