Skip to content
/ speculoos Public

Simple Javascript Class notation, CoffeeScript, TypeScript and ECMAScript 6 compliant!

Notifications You must be signed in to change notification settings

k33g/speculoos

Repository files navigation

#SpeculOOs ?

SpeculOOs is just a very simple Javascript Class notation, but ... CoffeeScript, TypeScript, ECMAScript 6 compliant ! (browser and nodejs)

You do not want to learn to CoffeeScript (or TypeScript oe ECMAScript 6) today, but it's interesting that your codes are compatible with them, if you finally decide to take the plunge. For example, your CoffeeScript (or TypeScript oe ECMAScript 6) classes will be able to inherit from your javascript classes.

##Install

type bower install k33g/speculoos#master

##Use

  • declare speculoos.js, if browser : <script src="speculoos.js"></script>, if nodejs : var speculoos = require("./speculoos.js");
  • write a class(es) :
/* this is javascript */
var Thing = speculoos.Class({
    constructor : function Thing (kind) { /* you have to name the constructor */
        this.kind = kind;
        
        /* private variable with getter and setter */
        var _nickName = '???';
        this.getNickName = function () { return _nickName; }
        this.setNickName = function (nickName) { _nickName = nickName; }
    },

    sayHello : function() {
        console.log("Hello !");
    }
})

var Human = speculoos.Class({
    extends : Thing, /* inheritance */
    constructor : function Human (name) {
        this.name = name;
        /* Call Parent constructor */
        Human.super.constructor.call(this, "I AM A HUMAN");
        Human.HumanCounter += 1;
    },

    toString : function() {
        return "Hello " + this.name;
    },

    sayHello : function() {
        /* Call Parent method */
        Human.super.sayHello.call(this);
        console.log("I'm "+this.name);
    },

    /* Static Members */
    $HumanCounter : 0,
    $getHumanCounter : function() { return Human.HumanCounter; }
});

###Use with Javascript

var bob = new Human('Bob');
console.log(bob, bob.toString(), Human.getHumanCounter(), Human.HumanCounter);
bob.sayHello();

##Prepare Future

... Just use extends

###Use Speculoos Classes with Coffeescript

### this is coffeescript ###
class SuperHero extends Human
    constructor:(name)->
        super name

    sayHello:->
        super
        console.log "And i'm a superhero : #{@getNickName()}"

superMan = new SuperHero 'Clark Kent'
superMan.setNickName "Super Man"

console.log superMan, superMan.toString(), SuperHero.getHumanCounter(), Human.HumanCounter
superMan.sayHello()

###Use Speculoos Classes with TypeScript

class SuperHero extends Human {
    constructor(name: string, nickName: string) {
        this.nickName = nickName;
        super(name);
    }
    
    sayHello() {
        super.sayHello();
        console.log("I am "+this.nickName)
    }

}
var peter = new SuperHero("Peter Parker", "SpiderMan");
peter.sayHello();

###Use Speculoos Classes with ECMAScript 6

class SuperHero extends Human {
  constructor(name, nickName) {
    super(name);
    this.setNickName(nickName);
  }

  sayHello() {
    super.sayHello();
    console.log(`I am ${this.nickName}`);
  }

}
var peter = new SuperHero("Peter Parker", "SpiderMan");

peter.sayHello();

... so simple ! No ? ...

##License

SpeculOOs is available under the terms of the MIT-License.

Copyright 2014-2015, Philippe Charrière

About

Simple Javascript Class notation, CoffeeScript, TypeScript and ECMAScript 6 compliant!

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published