Mochiscript Syntax

jeffsu edited this page Aug 14, 2012 · 10 revisions
Clone this wiki locally

Javascript IS Mochiscript

Mochiscript syntax is a superset of JavaScript's. This means that any JavaScript you write will run just fine in Mochiscript. Mochiscript simply adds extra features that make development life a little easier.

Object Oriented Features

Classes

Classes can be created with the "class" keyword. If you wish to have a custom initializer function, just include a method called "initialize".

class Hello {
  var defaultMessage = "Just say hello";

  function initialize(message) {
    this.message = message || this.defaultMessage;
  }

  function say() {
    console.log(this.message);
  }
}

var obj = new Hello("what's up?");

Instantiator

The method "initialize" is the default instance method used to instantiate an object (just like Ruby).

class Foo {
  function initialize(arg1, arg2) {
    this.args = [ arg1, arg2 ];
    console.log("Instance of Foo created!", arg1, arg2);
  }
}

var foo = new Foo("hello", "world");

Inheritance

class Goodbye extends Hello {
  var defaultMessage = "Goodbye";
  function say() {
    console.log("Saying:");
    this.$super();
  }
}

Mixins

module World {
  function world() {
    console.log('world');
  }
}

class HelloWorld extends Hello {
  include World;
}

Private Section

This is a little unorthodox part of Mochiscript which allows you to add a "closed" section that only methods in the scope of the class can access.

class MyClass {
  private {
    var CONSTANT_VAR = "constant";
  }

  function initialize(data) {
    this.data = data || CONSTANT_VAR;
  }
}

Accessing "self"

A lot of times, you need access to the "this" object in a callback. The problem is that "this" often points to something else in a different context. The workaround is usually:

class Foo {
  var hello = "hello";
  function setup() {
    var self = this;
    $('.hello').click(#{ alert(self.hello) }); 
  }
}

In mochiscript, it is no longer necessary to create a "self" variable. Its given to you in all methods:

class Foo {
  var hello = "hello";
  function setup() {
    $('.hello').click(#{ alert(self.hello) });
  }
}

Syntactic Sugar

Shorthand Functions "#{...}"

There are two ways to use this feature:

var myFunct = #{ console.log($1) }; // prints out first argument (supports up to 3 args)
var myFunct = #(msg){ console.log(msg) };

Shorthand for executing functions "##"

var val = ##{ return "val" };

Shorthand for "return"

Often, if you want to write inline functions, putting a "return" can make your line that much longer and undesirable to chain functions. Here is an alternative:

var orig = [ 1, 2, 3, 4 ];
var plusOne = orig.map(#{ => $1 + 1 }); 

Foreach

var array = [ 'hello', 'world' ];
foreach (var word in array) {
  console.log(word);
}

// foreach with iterator
foreach (var word:i in array) {
  console.log(i, word);
}

Heredocs

var message = <<END;
  this is a lot of text
  here.
END

Node-specific module exporters

Mochiscript has two helpers for exporting your files:

public class MyClass {

}

// equivalent to 
// exports.MyClass = MyClass;

Or make it the default export:

export class MyClass {

}

// equivalent to
// module.exports = MyClass;