Proto.js – prototypes as classes
JavaScript
Switch branches/tags
Nothing to show
Latest commit 3fd0db8 Oct 24, 2011 @rauschma rauschma Merge pull request #2 from rwldrn/update-example
Rename example code, Worker=>Employee. Fixes #1
Permalink
Failed to load latest commit information.
Proto.js Rename example code, Worker=>Employee. Fixes #1 Oct 23, 2011
README Rename example code, Worker=>Employee. Fixes #1 Oct 23, 2011

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 Employee = Person.extend({
        constructor: function (name, title) {
            Employee.super.constructor.call(this, name);
            this.title = title;
        },
        describe: function () {
            return Employee.super.describe.call(this)+" ("+this.title+")";
        }
    });

Interaction:

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

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

    > jane.describe()
    'Person called Jane (CTO)'