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` variablevar Animal =;var Snake =;var Horse =;var sam =tom =;samtom
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 <0.4kb minified (make report
: 467).
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.;// this defines a class that inherits from MySuperclass;// for shorthand, you can pass an object in lieu of the function argument,// but you lose the niceness of super and private methods.;MyClass = ;// instantiate objects by calling the class as a function// => init!, 1, 2// to initialize with varargs, use `apply` like any other function.var argsList = 1 2;MyClass // init!, 1, 2// you can use it like an idiomatic class:// `new` is optional, not really recommended.1 2 // => init!, 1, 2// non-pjs idiomatic subclass{ MyClass; }3 // => init!, 3, 33 instanceof MyClass // => true// `new` may be used to "force" instantiation when ambiguous,// for example in a factory method that creates new instancesMyClassprototype {return a b;};// because without `new`, `this.constructor(a, b)` is equivalent to// `MyClass.call(this, a, b)` which as we saw in the previous example// mutates `this` rather than creating new instances// allocate uninitialized objects with .Bare// (much like Ruby's Class#allocate)// nothing loggedinstanceof MyClass // => true// 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 prototypeMyClass = ;var myInst = ;MyClass;myInsta // => 2MyClass;// you can also use `.extend(definition)` to create new subclasses. This is equivalent// to calling P with two arguments.var Subclass = MyClass;
how do i use pjs in node.js?
Assuming you have it installed (via npm install pjs
), you can import it with
var P = 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
generatesbuild/p.min.js
-
make commonjs
generatesbuild/p.commonjs.js
, which is the same but hasexports.P = P
at the end -
make amd
generatesbuild/p.amd.js
, which is the same but hasdefine(P)
at the end -
make test
runs the test suite using the commonjs version. Requiresmocha
.