Code Monkey home page Code Monkey logo

stampit's Introduction

stampit

Stampit Build Status npm Gitter Twitter Follow UNPKG

Create objects from reusable, composable behaviors

Stampit is a 1.3KB gzipped (or 2.7K minified) JavaScript module which supports three different kinds of prototypal inheritance (delegation, concatenation, and functional) to let you inherit behavior in a way that is much more powerful and flexible than any other Object Oriented Programming model.

Stamps are standardised composable factory functions. Stampit is an infected compose featuring friendly handy API.

Find many more examples in this series of mini blog posts.

Example

import stampit from 'stampit'

const Character = stampit({
  props: {
    name: null,
    health: 100
  },
  init({ name = this.name }) {
    this.name = name
  }
})

const Fighter = stampit(Character, { // inheriting
  props: {
    stamina: 100
  },
  init({ stamina = this.stamina }) {
    this.stamina = stamina;    
  },
  methods: {
    fight() {
      console.log(`${this.name} takes a mighty swing!`)
      this.stamina--
    }
  }
})

const Mage = stampit(Character, { // inheriting
  props: {
    mana: 100
  },
  init({ mana = this.mana }) {
    this.mana = mana;    
  },
  methods: {
    cast() {
      console.log(`${this.name} casts a fireball!`)
      this.mana--
    }
  }
})

const Paladin = stampit(Mage, Fighter) // as simple as that!

const fighter = Fighter({ name: 'Thumper' })
fighter.fight()
const mage = Mage({ name: 'Zapper' })
mage.cast()
const paladin = Paladin({ name: 'Roland', stamina: 50, mana: 50 })
paladin.fight()
paladin.cast()

console.log(Paladin.compose.properties) // { name: null, health: 100, stamina: 100, mana: 100 }
console.log(Paladin.compose.methods) // { fight: [Function: fight], cast: [Function: cast] }

Status

Install

Via NPM: NPM

Via bower:

$ bower install stampit=https://npmcdn.com/[email protected]/dist/stampit.min.js
or
$ bower install stampit=https://unpkg.com/[email protected]/dist/stampit.min.js

Compatibility

Stampit should run fine in any ES5 browser or any node.js.

API

See https://stampit.js.org

What's the Point?

Prototypal OO is great, and JavaScript's capabilities give us some really powerful tools to explore it, but it could be easier to use.

Basic questions like "how do I inherit privileged methods and private data?" and "what are some good alternatives to inheritance hierarchies?" are stumpers for many JavaScript users.

Let's answer both of these questions at the same time.

// Some privileged methods with some private data.
const Availability = stampit().init(function() {
  var isOpen = false; // private

  this.open = function open() {
    isOpen = true;
    return this;
  };
  this.close = function close() {
    isOpen = false;
    return this;
  };
  this.isOpen = function isOpenMethod() {
    return isOpen;
  }
});

// Here's a stamp with public methods, and some state:
const Membership = stampit({
  props: {
    members: {}
  },
  methods: {
    add(member) {
      this.members[member.name] = member;
      return this;
    },
    getMember(name) {
      return this.members[name];
    }
  }
});

// Let's set some defaults:
const Defaults = stampit({
  props: {
    name: 'The Saloon',
    specials: 'Whisky, Gin, Tequila'
  },
  init({name, specials}) {
    this.name = name || this.name;
    this.specials = specials || this.specials;
  }
});

// Classical inheritance has nothing on this.
// No parent/child coupling. No deep inheritance hierarchies.
// Just good, clean code reusability.
const Bar = stampit(Defaults, Availability, Membership);

// Create an object instance
const myBar = Bar({name: 'Moe\'s'});

// Silly, but proves that everything is as it should be.
myBar.add({name: 'Homer'}).open().getMember('Homer');

For more examples see the API or the Fun With Stamps mini-blog series.

Development

Unit tests

npm t

Unit and benchmark tests

env CI=1 npm t

Unit tests in a browser

To run unit tests in a default browser:

npm run browsertest

To run tests in a different browser:

  • Open the ./test/index.html in your browser, and
  • open developer's console. The logs should indicate success.

Publishing to NPM registry

npx cut-release

It will run the cut-release utility which would ask you if you're publishing patch, minor, or major version. Then it will execute npm version, git push and npm publish with proper arguments for you.

stampit's People

Contributors

koresar avatar javascript-journal avatar ericelliott avatar troutowicz avatar fredyc avatar danielkcz avatar greenkeeperio-bot avatar greenkeeper[bot] avatar josephclay avatar joseherminiocollas avatar johnthad avatar unstoppablecarl avatar ruffle1986 avatar gfot avatar gitter-badger avatar tcrosen avatar sethlivingston avatar arikon avatar rudolf avatar paulfalgout avatar nkbt avatar angryobject avatar kurtpeters avatar jonboylailam avatar dwiyatci avatar fpoumian avatar eladchen avatar delapouite avatar boneskull avatar alexxnica avatar

Watchers

James Cloos 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.