Code Monkey home page Code Monkey logo

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.

cls's People

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

cls's Issues

couldn't use ng commands in angular2?

$ ng init
Cannot find module './models/config'
Error: Cannot find module './models/config'
at Function.Module._resolveFilename (module.js:455:15)
at Function.Module._load (module.js:403:25)
at Module.require (module.js:483:17)
at require (internal/module.js:20:19)
at Object. (C:\cygwin64\home\ishu.mishra97\frontpage\frontpage-angular2-app\node_modules\angular-cli\addon\ng2\index.js:4:16)
at Module._compile (module.js:556:32)
at Object.Module._extensions..js (module.js:565:10)
at Module.load (module.js:473:32)
at tryModuleLoad (module.js:432:12)
at Function.Module._load (module.js:424:3)

Add a .base property to class constructors for easy base class access

This would enable the following style as an alternative to this._super:

var SubClass = BaseClass.extend({
    init: function(arg) {
        // These should all be equivalent:
        SubClass.base.init.call(this, arg);
        SubClass.base.init.apply(this, arguments);
        this._super(arg);
        this._super.apply(this, arguments);
    }
});

// Just to be perfectly clear:
assert(SubClass.base === BaseClass.prototype);

Allow .name as a static property

Currently the constructor is a declared function with .name === "constructor", and populate(constructor, newStatics, base) is not able to override that property.

The constructor should be an anonymous function expression instead, so that one can set its .name property (or not) as one sees fit.

License?

Hey, can you add a license file to the repo? There are other modules that are requiring this one and they can't be used because this is an unlicensed module.

Thanks :)

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.