Skip to content

rauschma/class-js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Class.js – tools for standard inheritance in JavaScript

- Inspired by John Resig’s Simple Inheritance [1].
- Focus was on supporting constructor functions. Requirements:
  - Support the new operator
  - Support the instanceof operator
  - Support extending a class C via C.prototype
- For a completely new approach to JavaScript inheritance take a look at
  "prototypes as classes" [2]

[1] http://ejohn.org/blog/simple-javascript-inheritance/
[2] https://github.com/rauschma/proto-js

===== Code example =====

// Superclass
var Person = Class.extend({
    constructor: function (name) {
        this.name = name;
    },
    describe: function() {
        return "Person called "+this.name;
    }
});

// Subclass
var Worker = Person.extend({
    constructor: function (name, title) {
        Worker.super.constructor.call(this, name);
        this.title = title;
    },
    describe: function () {
        return Worker.super.describe.call(this)+" ("+this.title+")"; // (*)
    }
});
var jane = new Worker("Jane", "CTO");

About

Class.js – a simple JavaScript inheritance API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published