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.
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 560 bytes minified (see make report
).
why doesn't pjs suck?
Unlike some other frameworks out there, Pjs doesn't do any of this:
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:
P(function(proto, super, class, superclass) {
function myPrivateMethod(self, arg1, arg2) {
}
proto.init = function() {
myPrivateMethod(this, 1, 2)
};
return { thing: 3 };
});
P(MySuperclass, function(proto, super, class, superclass) {
proto.init = function() {
super.init.call(this);
};
});
P({ init: function(a) { this.thing = a } });
MyClass = P(function(p) { p.init = function(a, b) { console.log("init!", a, b) }; });
MyClass(1, 2)
var argsList = [1, 2];
MyClass.apply(null, argsList)
new MyClass(1, 2)
function Subclass(a) { MyClass.call(this, a, a); }
new Subclass(3)
new Subclass(3) instanceof MyClass
MyClass.prototype.clone = function(a, b) {
return new this.constructor(a, b);
};
new MyClass.Bare
new MyClass.Bare instanceof MyClass
MyClass = P({ a: 1 });
var myInst = MyClass();
MyClass.open(function(proto) { proto.a = 2 });
myInst.a
MyClass.open(function(proto, _super) { });
var Subclass = MyClass.extend({ a: 3 });
how do i use pjs in node.js?
Assuming you have it installed (via npm install pjs
), you can import it with
var P = require('pjs').P;
and go about your business.
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
.