GithubHelp home page GithubHelp logo

subclass's Introduction

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โšก, 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

  • [runtime]
    • Ext JS ( Ext.extend )
    • function-branch
    • npm search of inheritance, subclass or subtype gives you a lot of examples
  • [through compiler]
    • babel
    • TypeScript
    • CoffeeScript
    • Traceur
    • sweetjs
    • etc...

subclass's People

Contributors

greenkeeper[bot] avatar kt3k avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

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.