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 = ; // Wrap as Fac-Model. Only for the root object,// not the descendants, neat!var Animal = ; moduleexports = Animal;
Mammal.js (model extended from Animal)
var Animal = ;var Mammal = Animal; moduleexports = Mammal;
Dog.js (model extended from Mammal)
var Mammal = ;var Dog = Mammal; moduleexports = Dog;
dog.js (a copy of model Dog)
var Dog = ;var tobe = Dog; tobename // Tobetobeage // 5tobesex // boytobecolor // black tobe // boytobe // Hi from Tobetobe // Tobe running...tobe // 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 = ; // Wrap as Fac-Model. Only for the root object,// not the descendants, neat!var Base = ; moduleexports = Base;
Dance.js (general object)
var Dance = { return thisname + ' Dancing...'; }; moduleexports = Dance;
Fly.js (general object)
var Fly = { return thisname + ' Flying...'; }; moduleexports = Fly;
Super.js (model extended from Base, Fly, Dance)
var Base = ;var Fly = ;var Dance = ; var Super = Base; moduleexports = Super;
SuperDog.js (model extend from Mamml and Super)
var Mammal = ;var Super = ; var SuperDog = Mammal; moduleexports = SuperDog;
super-dog.js (a copy of model SuperDog)
var SuperDog = ;var dog = SuperDog; dogname // SuperTobedogage // 6dogsex // boydogcolor // blackdogcloak // red dog // Hi from SuperTobedog // SuperTobe running...dog // SuperTobe swimming... dog // SuperTobe flying...dog // SuperTobe dancing...dog // SuperTobe singing...dog // 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 x64Intel Core i7-4558U CPU @ 2.80GHz x 4Total Memory 16 GBNode.js v6.11.3V8 5.1.281.107--------------fac 30452 times/sec <= winnernative-class 12268 times/sec native-inheritance 5047 times/sec
See more details: Benchmark of performance
License
Copyright (c) 2017, Owen Luke