# mochiscript

> Javascript Dessert

Latest version **0.6.16** (published 2013-02-17) · 0 weekly downloads

## Install

```sh
npm install mochiscript
pnpm add mochiscript
yarn add mochiscript
bun add mochiscript
```

Provides the commands `ms-run`, `ms-parse`, `ms-compile`.

## Health

**Score 10/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; low quality score; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.6.16 |
| Published | 2013-02-17 |
| First published | 2011-09-26 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.4.0 |
| Dependencies | 1 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 30 |
| Author | Jeff Su |
| Maintainers | jeffsu, agate |
| Keywords | javascript, language, mochiscript, compiler |

## Links

- npm: https://www.npmjs.com/package/mochiscript
- Repository: https://github.com/jeffsu/mochiscript
- npm.io page: https://npm.io/package/mochiscript

## Dependencies (1)

- [watch](https://npm.io/package/watch.md) 0.5.1

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 0.6.16 (latest) — 2013-02-17
- 0.6.15 — 2012-10-19
- 0.6.14 — 2012-09-09
- 0.6.13 — 2012-09-07
- 0.6.12 — 2012-09-06
- 0.6.11 — 2012-09-05
- 0.6.10 — 2012-08-28
- 0.6.9 — 2012-08-14
- 0.6.8 — 2012-07-31
- 0.6.7 — 2012-06-30
- 0.6.6 — 2012-06-30
- 0.6.5 — 2012-06-30
- 0.6.4 — 2012-06-30
- 0.6.3 — 2012-06-25
- 0.6.2 — 2012-06-25
- … 38 more at https://npm.io/package/mochiscript/versions

## README

Summary
=======

Mochiscript is a superset of the JavaScript language that adds more Object-Oriented features such as: methods, inheritance, mixins, etc... What this means is that Mochiscript IS Javascript.  Mochiscript currently supports Ruby on Rails 3.1 (via asset pipeline) and Node.js.

More on Mochiscript features and syntax here: https://github.com/jeffsu/mochiscript/wiki/Mochiscript-Syntax

Installation
============

In Rails >3.1
-------------

In the Gemfile, add this line:

    gem 'mochiscript'

In app/assets/javascripts/application.js, add mochiscript

    //= require mochiscript
    
Now, you should be able to create ".ms" files and it'll be included as javascript using the new rails asset pipeline.

### An example:

Create a simple mochiscript (app/assets/javascripts/hello.ms):

    class Hello {
      function say() {
        alert('hello');
      }
    }

Now include it in application.js

    //= require hello

Now on your page, you should be able to put this code in after your javascript include section:

    <script>
      var obj = new Hello();
      obj.say();
    </script>

In Node.js
----------

    npm install mochiscript

In hello.ms:

    export class Hello {
      function say() {
        console.log("hello");
      }
    }

In main.js:
    
    require('mochiscript');
    var Hello = require('./hello');
    var obj   = new Hello();
    obj.say();

### CLI

Parsing a file:

    ms-parse <filename>

Running an ms file:
 
    ms-run <filename>

Compiling files in a directory:

    ms-watch <src> <dest> <template>

  1. src: source directory with .ms files
  1. dest: destination directory to write .js files
  1. template file (optional).  This will allow you to templatize mochiscript.  Just make sure the file has "__MOCHI__" in it.


Mochiscript in the browser
--------------------------

### Bootstrap File

Please include this file before requiring any mochiscript compiled file: [mochiscript.js](https://cloud.github.com/downloads/jeffsu/mochiscript/mochiscript.js)
  
### Middleware

Using connect/express:

    var options = {
      src: "views",
    };
    app.use(require('mochiscript').middleware(options));

## More on Syntax

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 returns

    [ 1, 2, 3 ].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

### Enumerable Functions (experimental)

    var greetings = [ 'hi', 'hello' ];
    var mapped    = greetings#map { $1 + " there!" };
    var some      = greetings#some { $1 == 'hi' };

## 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;

Authors
-------

  1. [Jeff Su](https://github.com/jeffsu)
  1. [Hong Hao](https://github.com/agate)

---
_Source: https://npm.io/package/mochiscript · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
