Socket
Socket
Sign inDemoInstall

cls

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cls

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


Version published
Weekly downloads
1.4K
decreased by-2.79%
Maintainers
1
Install size
13.4 kB
Created
Weekly downloads
 

Readme

Source

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.

Keywords

FAQs

Last updated on 26 May 2014

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc