Giter Club home page Giter Club logo

fac's Introduction

Fac.js

Linux Build Windows Build Test Coverage NPM Version License

Fac simplifies JavaScript programming. With Fac, we no longer need to simulate C++, Java or any other languages, no longer need to simulate classes and inheritance, just use object and extending.

Let us forget about object-oriented programming, forget to simulate object-oriented programming in JavaScript, and forget the following concepts: classes, inheritance, multiple inheritance, combined inheritance, parasitic inheritance, parasitic combination inheritance, and mixins.

Again, we just need object and extending. JavaScript programming should have been so simple. With Fac, all this is so natural. Fac is only 1KB gzipped with no dependency, and can be used in Node.js and in browsers. Fac is the abbreviation of "factory".

Install

In Node.js

$ npm install fac --save

Test:

$ npm test

 

In Web / RequireJS:

Download fac.js or fac.min.js.

 

Example

Animal.js (base object)

var fac = require('fac');

var Animal = {
    name: 'animal',
    sayHi: function(){
        console.log('Hi from ' + this.name);
    }
};

// Use fac() to pack the Object Animal, important.
// Append the methods new(), create(), extend() to the object Animal.
module.exports = fac(Animal);

Mammal.js (extended from Animal)

var fac = require('fac');
var Animal = require('./Animal');

// Animal extended to Mammal
var Mammal = Animal.extend({
  
    // Cover the name
    name: 'mammal', 
  
    // New method of Mammal
    run: function(){ 
        console.log('Running...');
    }
});

module.exports = fac(Mammal);

Dog.js (extended from Mammal)

var fac = require('fac');
var Mammal = require('./Mammal');

// Mammal extended to Dog
var Dog = Mammal.extend({
    name: 'dog',
    
    // For Initializing the instance of Dog.
    // If the Dog has init(), the name must be "init". 
    // Or, the Dog has no init().
    init: function(name, age, sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    },
  
    swim: function(){
        console.log('Swimming...');
    }
});

module.exports = fac(Dog);

dog.js (an instance of Dog)

var Dog = require('./Dog');
var dog = Dog('Tobe', 2, 'boy');  // Create an instance of Dog.
//var dog = Dog.new('Tobe', 2, 'boy');  // or .new(), as you wish.

dog.sayHi(); // Hi from Tobe
dog.run(); // Running...
dog.swim(); // Swimming...

console.log(dog.name); // Tobe
console.log(dog.age); // 2
console.log(dog.sex); // boy

 

Example of Multiple Extending

Base.js (base object)

var Base = {
    sing: function(){
        console.log('Singing...');
    }
};

module.exports = fac(Base);

Dance.js (base object)

var Dance = {
    dance: function(){
        console.log('Dancing...');
    }
};

module.exports = fac(Dance);

Fly.js (base object)

var Fly = {
    fly: function(){
        console.log('Flying...');
    }
};

module.exports = fac(Fly);

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

var Base = require('./Base');
var Fly = require('./Fly');
var Dance = require('./Dance');

var Super = Base.extend(Fly, Dance, {
    fight: function(){
        console.log('Fighting...');
    }
});

module.exports = fac(Super);

SuperDog.js (extend from Mamml and Super)

var Mammal = require('./Mammal');
var Super = require('./Super');

var Dog = Mammal.extend(Super, {
    name: 'super-dog',
  
    init: function(name, age, sex){
        this.name = name;
        this.age = age;
        this.sex = sex;
    },
  
    swim: function(){
        console.log('Swimming...');
    }
});

module.exports = fac(Dog);

super-dog.js (an instance of SuperDog)

var Dog = require('./SuperDog');
var dog = Dog('Super-Tobe', 2, 'boy');

dog.sayHi(); // Hi from Super-Tobe
dog.run(); // Running...
dog.swim(); // Swimming...

dog.fly(); // Flying...
dog.dance(); // Dancing...
dog.sing(); // Singing...
dog.fight(); // Fighting...

console.log(dog.name); // Super-Tobe
console.log(dog.age); // 2
console.log(dog.sex); // boy

 

API

Fac has only one method to pack object. For Eaxample:

var User = {
    init: function(username, password){
        this.username = username;
        this.password = password;
    }
};

// Pack User to User()
User = fac(User);

 

The Fac packed object has three apis:

  1. () and new(), their are equivalent:
// Use User() instead of Object.create(User).init(), neat!
var Jack = User('Jack', '123456');
var Rose = User.new('Rose', '123456');
  1. extend():
var Mammal = require('./Mammal');
var Super = require('./Super');

var Dog = Mammal.extend(Super, {
    init: function(name, age){
        this.name = name;
        this.age = age;
    }
});

 

The instance of the Fac packed object has only one api, isInstanceof():

var user = User(); 
console.log(user.isInstanceof(User)); // true

 

License

MIT

Copyright (c) 2017, Owen Luke

fac's People

Contributors

hiowenluke avatar

Watchers

James Cloos avatar Md Sarfaraz Hussain avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.