GithubHelp home page GithubHelp logo

benjamn / cls Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 0.0 11 KB

Class factory featuring inheritance of static properties, static constructors, lazy population of prototypes, and this._super

License: MIT License

JavaScript 100.00%

cls's Introduction

Introduction

Standardized class syntax is coming in ECMAScript 6, supposedly, but until then I need a class factory that fills the gap.

For me that means (in no particular order):

  • prototypal inheritance under the hood
  • access to overridden properties (super)
  • inheritance of static properties
  • static constructors
  • close correspondence to ES6 syntax
  • ES5/browser compatibility
  • only-pay-for-what-you-use performance
  • excellent test coverage: Build Status

I have no delusions of persuading the world to use this tool. Just try npm search inheritance some time to see how many other people have come up with solutions that work for them.

If you have a special interest in the tired me-too sport of pure-JavaScript class factory implementations, you might find this one interesting for its solutions to each of the requirements listed above, particularly the lazy (just-in-time) population of prototype properties.

Note that I did not mention privacy enforcement as a requirement. If you need a mechanism like the private keyword in other languages, I have a separate project that works seamlessly alongside this one.

Installation

From NPM:

npm install cls

From GitHub:

cd path/to/node_modules
git clone git://github.com/benjamn/cls.git
cd cls
npm install .

Usage

One example will have to suffice for now:

var cls = require("cls");

var BaseClass = cls.extend({
    init: function(a, b) {
        this.sum = a + b;
    },

    getSum: function() {
        return this.sum;
    },

    statics: {
        name: "BaseClass",

        init: function(cls) {
            cls.zero = new cls(0, 0);
        }
    }
});

var SubClass = BaseClass.extend({
    init: function(arg) {
        this._super(arg, arg);
        this.sum += 1;
    },

    statics: {
        name: "SubClass"
    }
});

assert(BaseClass.name === "BaseClass");
assert(SubClass.name === "SubClass");

assert(new BaseClass(2).getSum() === 4);
assert(new SubClass(2).getSum() === 5);

assert(SubClass.zero !== BaseClass.zero);
assert(SubClass.zero instanceof SubClass);
assert(SubClass.zero.getSum() === 1);

For more complex examples, see test/run.js.

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.