<p align='center'>
<a href='https://github.com/tasjs/fac' target='_blank'>
<img width='150' src='./res/logo_icon_150.png' /><br />
<img height='60' src='./res/slogan.png' />
</a>
</p>

<p align='center'>
<a href='https://travis-ci.org/tasjs/fac' target='_blank'><img src='https://img.shields.io/travis/tasjs/fac/master.svg?label=linux' alt='Linux Build Status' /></a>
<a href='https://ci.appveyor.com/project/tasjs/fac' target='_blank'><img src='https://img.shields.io/appveyor/ci/tasjs/fac/master.svg?label=windows' alt='Windows Build Status' /></a>
<a href='https://codecov.io/gh/tasjs/fac' target='_blank'><img src='https://img.shields.io/codecov/c/github/tasjs/fac/master.svg' alt='Coverage Status' /></a>
<a href='https://npmjs.org/package/fac' target='_blank'><img src='https://img.shields.io/npm/v/fac.svg' alt='Version' /></a>
<a href='https://npmjs.org/package/fac' target='_blank'><img src='https://img.shields.io/npm/l/fac.svg' alt='License' /></a>
</p>

　

Fac simplifies OOP. Fac wraps the object as Fac-Model which combines the advantages of class and object, is more better than both of them. Fac can be used in [Node.js](https://nodejs.org) and in browsers. "Fac" is the abbreviation of "factory".

Fac is more easy to use than class. Fac-Model uses model and copy instead of class and instance, abandont constructor and super. All descendants automatically inherit from parent and be initialized, without call and / or super.

Fac is more free than class. Fac-Model and its descendants can elegantly extend themselves or create a new model / copy directly, no need to create a subclass (or use assists like decorator) before that.

Fac is better than Object. As we know, the objects copied through Object.create or Object.assign will share the data for array and object, can not have separate data. Fac solved this problem.

Fac has a good [performance](./doc/performance/performance.md) to meet the development needs of most applications. For general application development, Fac is even faster than class. In creating a lot of children, Fac is faster than Object.assign, too. 



| Features                      | class | Object | Fac  |
| ----------------------------- | ----- | ------ | ---- |
| Inheritance                   | √     | √      | √    |
| Inheritance with elegant      |       |        | √    |
| Separate data                 | √     |        | √    |
| Default values                | √     | √      | √    |
| Default values with elegant   |       |        | √    |
| Self-extending                |       | √      | √    |
| Self-extending with elegant   |       | √      | √    |
| Create children from the copy |       | √      | √    |

　

## Install

In Node.js:
```bash
$ npm install fac --save
```

　

In Web / RequireJS:

Download [fac.js](./dist/fac.js) or [fac.min.js](./dist/fac.min.js).

　

## Test

Clone the Fac repo first:

```bash
$ git clone https://github.com/tasjs/fac.git
$ cd fac
$ npm install
```

Then run the tests in Node.js:
```bash
$ cd /path/to/fac
$ npm test
```

Or run the tests in your browser:
```bash
$ cd /path/to/fac
$ open test/web/index.html
```

　

## Demo

To run these demos, please clone the Fac repo and Install the development dependencies first (if you have not done it yet, see details in section "Test"). Then see the **examples/demo** folder. All demos are categorized according to Node.js and Web.

　

### Inheritance
![](./examples/demo/__res/inheritance-in-es5.png)

Run these demos in Node.js:

```bash
$ cd /path/to/fac
$ node examples/demo/nodejs/inheritance/inheritance.js
$ node examples/demo/nodejs/inheritance/fac-is-more-better.js
```

　

### Default values
![](./examples/demo/__res/default-values-in-es6.png)

Run these demos in Node.js:

```bash
$ cd /path/to/fac
$ node examples/demo/nodejs/default-values/class.js
$ node examples/demo/nodejs/default-values/fac-is-more-better.js
```

　

### Self-extending
![](./examples/demo/__res/self-extending.png)

Run these demos in Node.js:

```bash
$ cd /path/to/fac
$ node examples/demo/nodejs/self-extending/class.js
$ node examples/demo/nodejs/self-extending/fac-is-more-better.js
```

　

### Create children from the copy
![](./examples/demo/__res/create-children-from-the-copy.png)

Run these demos in Node.js:

```bash
$ cd /path/to/fac
$ node examples/demo/nodejs/create-children-from-the-copy/class.js
$ node examples/demo/nodejs/create-children-from-the-copy/fac-is-more-better.js
```

　

## Quick Example

### Inheritance

Animal.js (root model)
```js
var fac = require('fac');

// Wrap as Fac-Model. Only for the root object,
// not the descendants, neat!
var Animal = fac({
    name: 'Animal',
    age: 1,
    colors: ['white', 'black'],
    parts: {body: 1},

    init: function(){
        // The init function is not necessary in Fac.
        // If you wanna do something when .new() is called,
        // write it in init. If not, remove init.
    },

    sayHi: function(){
        return 'Hi from ' + this.name;
    }
});

module.exports = Animal;
```

　

Mammal.js (model extended from Animal)
```js
var Animal = require('./Animal');
var Mammal = Animal.extends({
    name: 'Mammal',
    sex: 'male',
    age: 2,

    default: function(){
        this.colors.push('gray');
        this.parts.mouth = 1;
        this.parts.eye = 2;
        this.parts.foot = 4;
    },

    init: function(){
        // The init function is not necessary in Fac.
        // If you wanna do something when .new() is called,
        // write it in init. If not, remove init.
    },

    run: function(){
        return this.name + ' running...';
    },

    getSex: function(){
        return this.sex;
    }
});

module.exports = Mammal;
```

　

Dog.js (model extended from Mammal)
```js
var Mammal = require('./Mammal');
var Dog = Mammal.extends({
    name: 'Dog',
    color: 'white',
    age: 3,

    swim: function(){
        return this.name + ' swimming...';
    }
});

module.exports = Dog;
```

　

**dog.js (a copy of model Dog)**
```js
var Dog = require('./Dog');
var tobe = Dog.new({name: 'Tobe', age: 5, sex: 'boy', color: 'black'});

tobe.name // Tobe
tobe.age // 5
tobe.sex // boy
tobe.color // black

tobe.getSex() // boy
tobe.sayHi() // Hi from Tobe
tobe.run() // Tobe running...
tobe.swim() // Tobe swimming...
```

　

Run this example in Node.js:

```bash
$ cd /path/to/fac
$ node examples/usage/nodejs/inheritance/Dog.test.js
```

　

### Multi-Extending

Base.js (root model)
```js
var fac = require('fac');

// Wrap as Fac-Model. Only for the root object,
// not the descendants, neat!
var Base = fac({
    sing: function(){
        return this.name + ' Singing...';
    }
});

module.exports = Base;
```

　

Dance.js (general object)
```js
var Dance = {
    dance: function(){
        return this.name + ' Dancing...';
    }
};

module.exports = Dance;
```

　

Fly.js (general object)
```js
var Fly = {
    fly: function(){
        return this.name + ' Flying...';
    }
};

module.exports = Fly;
```

　

Super.js (model extended from Base, Fly, Dance)
```js
var Base = require('./Base');
var Fly = require('./Fly');
var Dance = require('./Dance');

var Super = Base.extends(Fly, Dance, {
    fight: function(){
        return this.name + ' Fighting...';
    }
});

module.exports = Super;
```

　

SuperDog.js (model extend from Mamml and Super)
```js
var Mammal = require('./Mammal');
var Super = require('./Super');

var SuperDog = Mammal.extends(Super, {
    name: 'SuperDog',
    color: 'white',
    cloak: 'red',
    age: 4,

    init: function(){
        // The init function is not necessary in Fac.
        // If you wanna do something when .new() is called,
        // write it in init. If not, remove init.
    },

    swim: function(){
        return this.name + ' swimming...';
    }
});

module.exports = SuperDog;
```

　

**super-dog.js (a copy of model SuperDog)**
```js
var SuperDog = require('./SuperDog');
var dog = SuperDog.new({name: 'SuperTobe', age: 6, sex: 'boy', color: 'black'});

dog.name // SuperTobe
dog.age // 6
dog.sex // boy
dog.color // black
dog.cloak // red

dog.sayHi() // Hi from SuperTobe
dog.run() // SuperTobe running...
dog.swim() // SuperTobe swimming...

dog.fly() // SuperTobe flying...
dog.dance() // SuperTobe dancing...
dog.sing() // SuperTobe singing...
dog.fight() // SuperTobe fighting...
```

　

Run this example in Node.js:

```bash
$ cd /path/to/fac
$ node examples/usage/nodejs/multi-extending/SuperDog.test.js
```

　

## Full Examples

To run all examples, please clone the Tas repo and Install the development dependencies first (if you have not done it yet, see details in section "Test"). Then see the **examples/usage** folder. All examples and tests are categorized according to Node.js and Web.

　

## API

Fac wraps the general object as Fac-Model, then the Fac-Model and its descendants (including models and copies) have the following APIs. See the **examples/usage** folder for more usages.

　

| API          | Functions                                | Usage                                    |
| ------------ | ---------------------------------------- | ---------------------------------------- |
| .new()       | Create a new copy (child) of this model. | [Usage](./examples/demo/nodejs/inheritance/fac-is-more-better.js) |
| .extends()   | Extend to a new model (child) of this model. | [Usage](./examples/usage/nodejs/__models/Dog.js) |
| .ext()       | Extend this model itself.                | [Usage](./examples/usage/nodejs/self-extending/self-extending.test.js) |
| .spawn()     | Create a new copy (child) of this model (share the data for array and object). | [Usage](./examples/usage/nodejs/spawn/spawn.test.js) |
| this.super() | Call the method of parent or ancestor.   | [Usage](./examples/usage/nodejs/this.super/call-parent-method.js) |

　

#### Relationships of model and copy

| API          | Functions                 | Usage                                    |
| ------------ | ------------------------- | ---------------------------------------- |
| .isModelOf() | Is a model of some copy ? | [Usage](./examples/usage/nodejs/relationships/model-and-copy.test.js) |
| .isCopyOf()  | Is a copy of some model ? | [Usage](./examples/usage/nodejs/relationships/model-and-copy.test.js) |

　

#### Relationships of inheritance

| API               | Functions                                | Usage                                    |
| ----------------- | ---------------------------------------- | ---------------------------------------- |
| .isChildOf()      | Is a child (model or copy) of some object ? | [Usage](./examples/usage/nodejs/relationships/inheritance.test.js) |
| .isParentOf()     | Is a parent (model or copy) of some object ? | [Usage](./examples/usage/nodejs/relationships/inheritance.test.js) |
| .isDescendantOf() | Is a descendant (model or copy) of some object ? | [Usage](./examples/usage/nodejs/relationships/inheritance.test.js) |
| .isAncestorOf()   | Is an ancestor (model or copy) of some object ? | [Usage](./examples/usage/nodejs/relationships/inheritance.test.js) |

　

## Performance

Fac has a good performance to meet the development needs of most applications. For general application development, Fac is even faster than class. For example, run the following to compare the general inheritance:

```bash
$ cd /path/to/fac
$ node benchmark/general-inheritance.js
```

The results will be like below:

```bash
Platform info:
macOS Sierra 10.12 x64
Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz x 4
Total Memory 16 GB
Node.js v6.11.3
V8 5.1.281.107
--------------
fac                  30452    times/sec (100000 times, 3312 ms)  <= winner
native-class         12268    times/sec (100000 times, 8204 ms)
native-inheritance   5047     times/sec (100000 times, 20133 ms)
```

See more details: [Benchmark of performance](./doc/performance/performance.md)

　

## License

[MIT](LICENSE)

Copyright (c) 2017, Owen Luke
