1.3.0 • Published 9 years ago

subclassjs v1.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
9 years ago

subclassjs v1.3.0

Easy simple class inheritance tool, just works perfect in es5.

For those who don't like to use "compilers" but like to use class.

  • Class definition by a function:zap:, not by an object.
  • instanceof works.
  • Sets prototype.constructor properly
  • parent method call support
  • no global side effect

Usage

Basic

subclass = require('subclassjs');

// define a class
var Child = subclass(Parent, function (pt) {

    pt.constuctor = function () { /* blah */ };

    pt.myMethod = function () { /* ... */ };

    //...

});

The first argument pt of the defining function is the prototype object of the child class to be defined. You can define methods of the child class by setting the properties on it.

Default parent

You can omit first argument. If it's omitted, it inherits Object class.

var MyClass = subclass(function (pt) { /* ... */ });

The above is the same as the below;

var MyClass = subclass(Object, function (pt) { /* ... */ });

Inheritance

You can inherit classes. The second parameter of the defining function is parent's prototype (not the parent class itself) and you can call parent's method through it.

var ChildClass = subclass(ParentClass, function (pt, parent) {

    pt.constructor = function () {

        parent.constructor.apply(this, arguments);

        // do something

    };

    pt.myMethod = function () {

         var result = parent.myMethod.apply(this, arguments);

         // do something

         return result;

    };

});

Static methods

You can define static methods.

var ChildClass = subclass(ParentClass, function (pt) {

    pt.constructor.staticMethod = function (str) {

        // ...

    };

});

In this case, you can call ChildClass.staticMethod()

Other approaches to write classes

1.3.0

9 years ago

1.2.0

9 years ago

1.1.1

9 years ago

1.1.0

9 years ago

1.0.0

9 years ago