Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Classes for javascript that don't suck.
JavaScript CoffeeScript
Pull request Compare This branch is 55 commits behind master.

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
build
examples
src
test
.gitignore
.jshintrc
.travis.yml
LICENSE
Makefile
README.md
index.js
package.json

README.md

Build Status

P.js

P.js is a lightweight layer over javascript's built-in inheritance system that keeps all the good stuff and hides all the crap.

just show me some code already

Okay.

// adapted from coffeescript.org
// P.js exposes the `P` variable
var Animal = P(function(animal) {
  animal.init = function(name) { this.name = name; };

  animal.move = function(meters) {
    console.log(this.name+" moved "+meters+"m.");
  }
});

var Snake = P(Animal, function(snake, animal) {
  snake.move = function() {
    console.log("Slithering...");
    animal.move.call(this, 5);
  };
});

var Horse = P(Animal, function(horse, animal) {
  horse.move = function() {
    console.log("Galloping...");
    animal.move.call(this, 45);
  };
});

var sam = Snake("Sammy the Python")
  , tom = Horse("Tommy the Palomino")
;

sam.move()
tom.move()

how is pjs different from X

Most class systems for JS let you define classes by passing an object. P.js lets you pass a function instead, which allows you to closure private methods and macros. It's also 559 bytes minified (see make report).

why doesn't pjs suck?

Unlike some other frameworks out there, Pjs doesn't do any of this:

  • interfaces, abstract static factory factories, and other bloat
  • use Object.create (it even works in IE < 8!)
  • break instanceof
  • hack functions onto this at runtime
  • rely on magical object keys which don't minify (the only special name is init)

what can i do with pjs?

  • inheritable constructors (via the optional init method)
  • closure-based "private" methods (see below)
  • easily call super on public methods without any dirty hacks
  • instantiate your objects without calling the constructor (absolutely necessary for inheritance)
  • construct objects with variable arguments

how do i use pjs?

You can call P in a few different ways:

// this defines a class that inherits directly from Object.
P(function(proto, super, class, superclass) {
  // define private methods as regular functions that take
  // `self` (or `me`, or `it`, or anything you really want)
  function myPrivateMethod(self, arg1, arg2) {
    // ...
  }

  proto.init = function() {
    myPrivateMethod(this, 1, 2)
  };

  // you can also return an object from this function, which will
  // be merged into the prototype.
  return { thing: 3 };
});

// this defines a class that inherits from MySuperclass
P(MySuperclass, function(proto, super, class, superclass) {
  proto.init = function() {
    // call superclass methods with super.method.call(this, ...)
    //                           or super.method.apply(this, arguments)
    super.init.call(this);
  };
});

// for shorthand, you can pass an object in lieu of the function argument,
// but you lose the niceness of super and private methods.
P({ init: function(a) { this.thing = a } });

MyClass = P(function(p) { p.init = function(a, b) { console.log("init!", a, b) }; });
// instantiate objects by calling the class
MyClass(1, 2) // => init!, 1, 2

// `new` is optional, not really recommended.
new MyClass(1, 2) // => init!, 1, 2

// allocate uninitialized objects with .Bare
// (much like Ruby's Class#allocate)
new MyClass.Bare // nothing logged
new MyClass.Bare instanceof MyClass // => true

// to initialize with varargs, use `apply` like any other function.
var argsList = [1, 2];
MyClass.apply(null, argsList) // init!, 1, 2

// you can use `.open` to reopen a class.  This has the same behavior
// as the regular definitions.
// note that _super will still be set to the class's prototype
MyClass = P({ a: 1 });
var myInst = MyClass();
MyClass.open(function(proto) { proto.a = 2 });
myInst.a // => 2
MyClass.open(function(proto, _super) { /* _super is Object.prototype here */ });

// you can use `.mixin` to extend the prototype chain.  This is similar
// to .open, but _super works normally, and it doesn't affect existing instances.
MyClass.mixin({ a: 3 });
myInst.a // => 2
MyClass.mixin(function(proto, _super) { proto.foo = function() { return _super.a }; });
MyClass().foo() // => 3

what is all this Makefile stuff about

It's super useful! In addition to make, Pjs uses some build tools written on Node. With the Node Package Manager that comes with recent versions of it, just run

npm install

from the root directory of the repo and make will start working.

Here are the things you can build:

  • make minify generates build/p.min.js

  • make commonjs generates build/p.commonjs.js, which is the same but has exports.P = P at the end

  • make amd generates build/p.amd.js, which is the same but has define(P) at the end

  • make test runs the test suite using the commonjs version. Requires mocha.

(I tested these tasks with GNU make. If someone could verify this all works with BSD make (like on a Mac) that'd be awesome.)

Something went wrong with that request. Please try again.