Jangaroo

My former colleagues at CoreMedia decided to release their Javascript cross-compiler, Jangaroo, as open source. The tool takes Javascript 2 / ActionScript 3 and translates it into browser-compatible JavaScript 1.x, allowing the developer to write the scripts in a much nicer — and more object-oriented language.

Here’s a sample JavaScript 2.0 class:

package net.jangaroo.example {

  /**
   * A simple example demonstrating how to receive and return typed arguments.
   */
  public class HelloWorld {

    /**
     * Returns a personalized greeting.
     * @param name name of the person to greet
     * @return String a personalized greeting
     */
    public function greet(name : String) : String {
      return "Hello, " + name + "!";
    }

    /**
     * Returns a personalized and HTML-escaped greeting.
     * @param name name of the person to greet
     * @return String a personalized and HTML-escaped greeting
     */
    public function greetHtml(name : String) : String {
      return this.greet(name).replace(/&/g, "&").
                                replace(/</g, "&lt;");
    }
  }
}

Once translated with the cross-compiler, you can import the classes into your HTML and use them:

 <script type="text/javascript" src="joo/Class.js"></script>
<script type="text/javascript" src="net/jangaroo/example/HelloWorld.js"></script>
<script type="text/javascript">
//<![CDATA[

// asserts that all required classes have been loaded
joo.Class.complete();

// instantiate the example class
var helloWorld = new net.jangaroo.example.HelloWorld();

// button onclick event handler
function sayHello() {
// grab the input and output elements
var outputDiv = document.getElementById("output");
var inputField = document.getElementById("nameField");

// call the #greetHtml method of the HelloWorld class, and output the return value
outputDiv.innerHTML = helloWorld.greetHtml(inputField.value);
}
// ]]>
</script>

Check it out on jangaroo.net.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Technorati
  • description
  • Facebook
  • LinkedIn
  • Slashdot

Comments

Leave a Reply

You must be logged in to post a comment.

.