factory-knows

An instanceof alternative for non class based, factory oriented, programming.

Usage no npm install needed!

<script type="module">
  import factoryKnows from 'https://cdn.skypack.dev/factory-knows';
</script>

README

factory-knows 🏭👃

Build Status Coverage Status Greenkeeper badge WebReflection status

An instanceof alternative for non class based, factory oriented, programming.

import $ from 'factory-knows';
// OR
// const $ = require('factory-knows');
// OR
// <script src="https://unpkg.com/factory-knows"></script>
// <script>const Factory = factoryKnows(...);</script>

const Person = $((name = 'anonymous') => ({
  birthday() {
    this.age++;
    console.log('🎉');
  },
  age: 0,
  name
}));

const me = Person('A G');
Person.knows(me); // true
me.birthday();    // 🎉

const {assign} = Object;
const Employee = $((company, name) => assign(
  Person(name),
  {
    levelUP() {
      this.experience++;
      console.log('🤝');
    },
    experience: 0,
    company
  }
));

const roadrunner = Employee('ACME', 'RoadRunner');
Person.knows(roadrunner);   // true
Employee.knows(roadrunner); // true
roadrunner.levelUP();       // 🤝
roadrunner.experience;      // 1