Coder Social home page Coder Social logo

supertype's Introduction

superType

Purpose

superType is a type system for javascript that supports

  • Classical inheritence

  • Mixins

  • Composition including collections

Installation

Include lib/index.js for use in the browser. Install on node via npm:

npm install supertype

It is automatically installed as a dependency for Amorphic

Example

Classes are defined as "templates".

ObjectTemplate = require('supertype');

Animal = ObjectTemplate.create("Animal",
{
    name: {type: String},
    isMammal: {type: Boolean, value: true},
    legs: {type: Number, value: 2}
});

Lion = Animal.extend("Lion",
{
    init: function () {
        Animal.call(this);
        this.name = "Lion";
        this.legs = 4;
    },
    canRoar: function () {return true}
});

Bear = Animal.extend("Bear",
{
    init: function () {
        Animal.call(this);
        this.name = "Bear";
    },
    canHug: function () {return true}
});

Ark = ObjectTemplate.create("Ark",
{
    animals: {type: Array, of: Animal, value: []},
    board: function (animal) {
        animal.ark = this;
        this.animals.push(animal)
    }
});
Animal.mixin(
{
    ark:    {type: Ark}
});

You create objects using new

var ark1 = new Ark();
ark1.board(new Lion());
ark1.board(new Bear());

var ark2 = new Ark();
ark2.board(new Lion());
ark2.board(new Bear());

Because superType knows about the interrelationships between your objects you can serialize and de-serialize even though you have circular references

var serialArk1 = ark1.toJSONString();
var serialArk2 = ark2.toJSONString();
ark1 = Ark.fromJSON(serialArk1);
ark2 = Ark.fromJSON(serialArk2);

License

superType is licensed under the MIT license

supertype's People

Contributors

selsamman avatar csmastermind avatar hackerrdave avatar

Watchers

James Cloos avatar swisslala avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.