flairjs

0.59.75 • Public • Published

Flair.js

True object orientated features for plain vanilla JavaScript

Build Status Dependencies Dev Dependencies Known Vulnerabilities Issues
MIT license npm version GitHub top language GitHub file size in bytes GitHub last commit GitHub (Pre-)Release Date StackOverflow

Introduction

JavaScript is everywhere, and its popularity has grown tremendously. There has been several enhancements done in the language to make it more powerful. ES6/ES7 has added several object-oriented features to the language, but the bottleneck has always been the varied support of these features by web browsers, thus restricting their large-scale usage.

Due to rich and long history of JavaScript, several compatibility issues do exists. Expecting to have all awesome object-oriented concepts as they exists in any new languages like C#, Java, etc., is tough. Of course there is TypeScript, but that is another language. Even though it 'compiles' or 'transpiles' to native JavaScript, it is not 'the' JavaScript.

Flair.js takes the problem head-on and brings majority of the awesomeness of C#/Java features in JavaScript, natively!

Basic object oriented concepts like, inheritance, encapsulation, polymorphism, events, together with advance features like aspect oriented and attribute based programming, custom attributes, serialization, dependency injection and reflection, etc. are all nicely baked in this JavaScript library.

Furthermore, all of these are available via pure JavaScript syntax, without any build-time transpilation or compilation of your codebase. What you write, is what gets executed. No change!

The forward-looking and future-proof design of Flair.js, plays well with ongoing ECMAScript advancements. While executing code, behind the scenes, new JavaScript constructs are used wherever possible.

Flair.js works in web browsers and in other JavaScript environments like Node and NW.js.

Features

  • Pure JavaScript, No external dependencies, ~25k min+gz.
  • Inheritance: Single inheritance chain, Multiple inheritance via Mixins, Restrictions via 'sealed', etc.
  • Encapsulation: True Public, Private and Protected members etc.
  • Polymorphism: Abstract classes, Interfaces, Method overloading and overriding, Restrictions via 'sealed', Dynamic casting via 'as', etc.
  • Mature Base Types: Class, Struct, Interface, Mixin, Enum, Exception, Resource, Assembly, AppDomain, etc.
  • Aspect orientation: Aspect definitions with Before, After and Around advise weaving on methods.
  • Attributes based programming: Inbuilt system attributes like 'readonly', 'sealed' and many more with full-blown support of defining custom attributes and its usage over class and class members.
  • Dependency injection: Object life-cycle management via DI container and constructor, method and property injection of other types.
  • Serialization: Seamless serialization and deserialization of class objects for persistance and transfer.
  • Reflection: Meta programming made easy with advance reflection support on all live objects and types.
  • Type organization: Organization of types under individual namespaces and assemblies which are loaded in specific assembly ;load context under primary or secondary app domains.
  • Others: Singleton, Static classes and members, State storage, Event handling, Async method calls, Auto-disposable objects, deprecate member notifications, Telemetry, Extension ports, etc.

Getting Started

1. Install

Install using npm install flairjs or download latest release. All you need is to have flair.min.js available, whatever approach you want to take.

2. Include

Include Flair.js in your html page or load it as a module, and initialize.

There are no external dependencies of this library, therefore feel free to include in whatever order required. However this must be loaded before any *.js file which uses Flair.js features, for those to be available.

When using on client side:

<script type="text/javascript" src="path-to-flair/flair.min.js"></script>

Flair.js also support module loaders and can be loaded via require or other module loading techniques.

When using on server side:

const flair = require('flairjs');

3. Play with Objects

With flair objects available, JavaScript now has the awesomeness of C#/Java. Define and play with objects.

Here is a very basic example:

 
// note: when code is wrapped in a flair Assembly, such imports are not required
const { Class, $$, event, using } = flair; 
 
// define Vehicle class
let Vehicle = Class('Vehicle', function() {
    
    // constructor
    this.construct = (capacity) => {
        this.cc = capacity;
        console.log('Vehicle constructed!');    
    };
    
    // property
    this.cc = 0;
 
    // method
    this.start = () => {
        // raise event with current time of start
        this.started(Date.now());
    };
 
    // event
    this.started = event((time) => {
        return { when: time }; // event args
    });
});
 
 
// define Car, derived from Vehicle
let Car = Class('Car', Vehicle, function() {
    
    $$('override'); // constructor overriding
    this.construct = (base, model, capacity) => {
        // call base class's constructor
        base(capacity);
 
        // note: model is readonly, but can still be defined in constructor
        this.model = model;
 
        // subscribe to started event of base class
        this.started.add(this.onStarted);
        
        console.log('Car constructed!');    
    });
 
    $$('readonly'); // readonly property
    this.model = '';
 
    // private event handler
    $$('private');
    this.onStarted = (e) => {
        // read event args, this and parent class properties
        console.log(`${this.model} (${this.cc}cc) ${e.name} at: ${e.args.when}`);
    };
 
    // dispose car via destructor
    this.dispose = () => {
        console.log('Car disposed!');
    };     
});
 
 
// auto disposable block
using(new Car('SUV', 3000), (suv) => {
    suv.start();
});
 

Executing above code will show following on console:

Vehicle constructed!

Car constructed!

SUV (3000cc) started at: (time)

Car disposed!

Explore The Power

What you have seen above is the tip of the iceberg. Flair.js adds a lot of firepower to JavaScript that makes building complex JavaScript projects as easy as with C# or Java.

To tap the real power of Flair.js, explore the Guides to understand concepts and behaviors, API to know details of the programming interface for various constructs, and finally Examples to see working code, showcasing all the key concepts in action.

However, before you delve deep into any of these areas, begin with getting an Overview first.

Release History

See the changelog here.

License

Copyright © 2017-2019 Vikas Burman.
Released under the terms of the MIT license. Authored and maintained by Vikas Burman.

Package Sidebar

Install

npm i flairjs

Homepage

flairjs.com

Weekly Downloads

56

Version

0.59.75

License

MIT

Unpacked Size

504 kB

Total Files

6

Last publish

Collaborators

  • vikasburman