Home

Jeff Su edited this page Sep 15, 2011 · 12 revisions

JS2- JavaScript Sugar

JS2 is syntactic sugar on top of JavaScript. It adds common programming functionality to JavaScript that was inspired by other languages (Java, Ruby, Perl). Since JS2 is a superset of JavaScript, JavaScript remains fully compatible with JS2. JS2 is available in 4 different flavors and is written in JavaScript:

Example:

class Vehicle {
  function move() {
    return "I move";
  }
}

class Car extends Vehicle {
  function move() {
    return "I roll";
  }
}

class SportsCar extends Car {
  function move() {
    return this.$super + ' quickly';
  }
}

var ferrari = new SportsCar();
console.log(ferrari.move());

Who is it for?

JS2 was designed to service various JavaScript environments. You can use it whether you are using a client-side browser JavaScript or a server-side implementation such as ringojs or nodejs. There are, however, different strategies.

Quick Setup

Install JS2: (here)Installation

gem install js2
# or
npm install js2
# or
ringo-admin install jeffsu/js2-ringo

Create project directory structure

mkdir -p myproj/lib myproj/src

Create js2 files

vi myproj/src/Foo.js2

Installation

For more detailed instructions, go here.

For Ruby (using Google Chrome's V8 engine)

# for ruby mri
gem install rubyracer
gem install js2

For NodeJS

This is the fastest implementation and is the preferred way to run compilations First, install nodejs and npm (node's package manager)

npm install js2

For RingoJS

First, install ringo.

ringo-admin install jeffsu/js2-ringo

More information can be found here: Syntax