class-js
class-js copied to clipboard
Class.js – a simple JavaScript inheritance API
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");