fac

    1.1.3 • Public • Published


    Linux Build Status Windows Build Status Coverage Status Version License

    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 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 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:

    $ npm install fac --save

    In Web / RequireJS:

    Download fac.js or fac.min.js.

    Test

    Clone the Fac repo first:

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

    Then run the tests in Node.js:

    cd /path/to/fac
    $ npm test

    Or run the tests in your browser:

    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

    Run these demos in Node.js:

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

    Default values

    Run these demos in Node.js:

    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

    Run these demos in Node.js:

    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

    Run these demos in Node.js:

    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)

    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)

    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)

    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)

    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:

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

    Multi-Extending

    Base.js (root model)

    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)

    var Dance = {
        dance: function(){
            return this.name + ' Dancing...';
        }
    };
     
    module.exports = Dance;

    Fly.js (general object)

    var Fly = {
        fly: function(){
            return this.name + ' Flying...';
        }
    };
     
    module.exports = Fly;

    Super.js (model extended from Base, Fly, Dance)

    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)

    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)

    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:

    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
    .extends() Extend to a new model (child) of this model. Usage
    .ext() Extend this model itself. Usage
    .spawn() Create a new copy (child) of this model (share the data for array and object). Usage
    this.super() Call the method of parent or ancestor. Usage

    Relationships of model and copy

    API Functions Usage
    .isModelOf() Is a model of some copy ? Usage
    .isCopyOf() Is a copy of some model ? Usage

    Relationships of inheritance

    API Functions Usage
    .isChildOf() Is a child (model or copy) of some object ? Usage
    .isParentOf() Is a parent (model or copy) of some object ? Usage
    .isDescendantOf() Is a descendant (model or copy) of some object ? Usage
    .isAncestorOf() Is an ancestor (model or copy) of some object ? Usage

    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:

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

    The results will be like below:

    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

    License

    MIT

    Copyright (c) 2017, Owen Luke

    Install

    npm i fac

    DownloadsWeekly Downloads

    25

    Version

    1.1.3

    License

    MIT

    Last publish

    Collaborators

    • avatar