Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 Proto.js
Octocat-spinner-32 README
README
Proto.js – prototypes as classes

The core idea of Proto.js is that many things become simpler in JavaScript if you make the prototype the class and not the constructor.
Details: http://www.2ality.com/2011/06/prototypes-as-classes.html

Code:

    // Superclass
    var Person = Proto.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+")";
        }
    });

Interaction:

    var jane = Worker.new("Jane", "CTO"); // normally: new Worker(...)

    > Worker.isPrototypeOf(jane) // normally: jane instanceof Worker
    true

    > jane.describe()
    'Person called Jane (CTO)'
Something went wrong with that request. Please try again.