All Projects → stampit-org → Stampit

stampit-org / Stampit

Licence: mit
OOP is better with stamps: Composable object factories.

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects
HTML
75241 projects

Projects that are alternatives of or similar to Stampit

BashClass
BashClass is an Object Oriented Programming language that compiles to BASH 4.4
Stars: ✭ 40 (-98.68%)
Mutual labels:  oop, class, object-oriented
Container Ioc
Inversion of Control container & Dependency Injection for Javascript and Node.js apps powered by Typescript.
Stars: ✭ 89 (-97.05%)
Mutual labels:  dependency-injection, factory
Solid
Книга о принципах объектно-ориентированного дизайна SOLID
Stars: ✭ 280 (-90.73%)
Mutual labels:  dependency-injection, oop
vesselize
⛵ A JavaScript IoC container that works seamlessly with Vue.js and React.
Stars: ✭ 22 (-99.27%)
Mutual labels:  dependency-injection, composition
Factory
Factory for object creation and dependency injection. Works with normal C# apps or under Unity3d
Stars: ✭ 50 (-98.34%)
Mutual labels:  dependency-injection, factory
Danf
Danf is a Node.js full-stack isomorphic OOP framework allowing to code the same way on both client and server sides. It helps you to make deep architectures and handle asynchronous flows in order to help in producing scalable, maintainable, testable and performant applications.
Stars: ✭ 58 (-98.08%)
Mutual labels:  dependency-injection, oop
Design-Patterns
Project for learning and discuss about design patterns
Stars: ✭ 16 (-99.47%)
Mutual labels:  oop, object-oriented
Python And Oop
Object-Oriented Programming concepts in Python
Stars: ✭ 123 (-95.93%)
Mutual labels:  oop, class
SteroidsDI
Advanced Dependency Injection to use every day.
Stars: ✭ 15 (-99.5%)
Mutual labels:  factory, dependency-injection
python-pyfields
Define fields in python classes. Easily.
Stars: ✭ 39 (-98.71%)
Mutual labels:  oop, class
okito
Your best flutter coding friend. All in one; state management, navigation management(with dynamic routing), local storage, localization, dependency injection, cool extensions with best usages and with the support of best utilities!
Stars: ✭ 37 (-98.78%)
Mutual labels:  dependency-injection, class
Object Oriented Programming Using Python
Python is a multi-paradigm programming language. Meaning, it supports different programming approach. One of the popular approach to solve a programming problem is by creating objects. This is known as Object-Oriented Programming (OOP).
Stars: ✭ 183 (-93.94%)
Mutual labels:  oop, class
Lw oopc
modified from http://sourceforge.net/projects/lwoopc/
Stars: ✭ 159 (-94.74%)
Mutual labels:  oop, object-oriented
Python Dependency Injector
Dependency injection framework for Python
Stars: ✭ 1,203 (-60.18%)
Mutual labels:  dependency-injection, factory
Cohesion
A tool for measuring Python class cohesion.
Stars: ✭ 129 (-95.73%)
Mutual labels:  oop, class
Testdeck
Object oriented testing
Stars: ✭ 206 (-93.18%)
Mutual labels:  dependency-injection, oop
Not Awesome Es6 Classes
A curated list of resources on why ES6 (aka ES2015) classes are NOT awesome
Stars: ✭ 1,185 (-60.77%)
Mutual labels:  composition, class
Solrb
Solr + Ruby + OOP + ❤️ = Solrb
Stars: ✭ 37 (-98.78%)
Mutual labels:  oop, object-oriented
kaop
Advanced OOP Library with createClass, inheritance, providers, injectors, advices which enables handy Inversion of Control techniques
Stars: ✭ 40 (-98.68%)
Mutual labels:  dependency-injection, composition
movie-booking
An example for booking movie seat, combined of Android Data Binding, State Design Pattern and Multibinding + Autofactory. iOS version is: https://github.com/lizhiquan/MovieBooking
Stars: ✭ 80 (-97.35%)
Mutual labels:  factory, dependency-injection

stampit

Stampit npm Gitter Twitter Follow UNPKG

Create objects from reusable, composable behaviors

Stampit is a 1.4KB gzipped (or 3K 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 a handy implementation of the specification featuring friendly API.

Find many more examples in this series of mini blog posts or on the official website.

Example

import stampit from 'stampit'

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

const Fighter = Character.compose({ // 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 = Character.compose({ // 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

NPM

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() {
    let 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].