• Stars
    star
    195
  • Rank 198,180 (Top 4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 13 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Classes for javascript that don't suck.

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 <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.
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 as a function
MyClass(1, 2) // => init!, 1, 2

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

// you can use it like an idiomatic class:
// `new` is optional, not really recommended.
new MyClass(1, 2) // => init!, 1, 2
// non-pjs idiomatic subclass
function Subclass(a) { MyClass.call(this, a, a); }
new Subclass(3) // => init!, 3, 3
new Subclass(3) instanceof MyClass // => true

// `new` may be used to "force" instantiation when ambiguous,
// for example in a factory method that creates new instances
MyClass.prototype.clone = function(a, b) {
  return new this.constructor(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)
new MyClass.Bare // nothing logged
new MyClass.Bare instanceof 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 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 also use `.extend(definition)` to create new subclasses.  This is equivalent
// to calling P with two arguments.
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.

More Repositories

1

parsimmon

A monadic LL(infinity) parser combinator library for javascript
JavaScript
1,240
star
2

balls

Bash on Balls
Shell
860
star
3

ry

The simplest ruby version manager
Shell
134
star
4

python-cache

Caching for humans
Python
63
star
5

variants-slides

Slides for "Variants are Not Unions", a talk given at Clojure Conj 2014
Clojure
16
star
6

ragel.vim

My "fork" of Adrian Thurston's syntax file
Vim Script
15
star
7

mock_redis

A little class that acts like redis-rb, but for testing on machines without redis.
Ruby
14
star
8

express-handlebars

Handlebars integration for express
JavaScript
13
star
9

color.js

The missing color library
JavaScript
9
star
10

tnetstrings-js

A tnetstrings parser for javascript (browser or node)
CoffeeScript
9
star
11

cinch-test

A testing module for the cinch IRC framework
Ruby
8
star
12

ixl-prototype

A prototype of the ixl language.
Haskell
7
star
13

eco.vim

Vim syntax highlighting for eco, embedded coffeescript DEPRECATED: see https://github.com/kchmck/vim-coffee-script/blob/master/syntax/eco.vim
Vim Script
7
star
14

rpgem

The gem-to-rpm converter, done right
Ruby
5
star
15

lolgebra

A chat framework where you can lol in math!
Ruby
5
star
16

iplogic

IPv4 swiss army chainsaw.
Ruby
5
star
17

math-tau

Ruby
4
star
18

ix

a lightweight, easily configurable modal editor
4
star
19

jim-ncurses

ncurses bindings for jimtcl
C
4
star
20

back

A middleware framework for bash CGI scripts
Shell
4
star
21

rudis

An extensible OO redis client for ruby
Ruby
3
star
22

dot

my dotfiles
Vim Script
3
star
23

thunk.js

A generator for trampoline-evaluated recursive algorithms.
JavaScript
3
star
24

netherpad

An online, plugin-friendly, shared document in node (wip)
3
star
25

netherquill

Share documents!
3
star
26

nathans-university-jayferd

Exercises for Nathan's University courses.
JavaScript
3
star
27

tweet.sh

a twitter client implemented in bash
Shell
3
star
28

express-contrib

Express framework utilities
JavaScript
3
star
29

cube

A Rubik's Cube emulator in Haskell, for testing solving algorithms and such.
Haskell
3
star
30

class.sh

Classical OO for bash (yeah you heard me)
Shell
2
star
31

workqueue

a dirt-simple in-process aggregating workqueue for parallel processing in Ruby.
Ruby
2
star
32

node-named-routes

framework-agnostic named routes for node
JavaScript
2
star
33

clojure-west-2015-slides

Groff
2
star
34

thankful_eyes.vim

A vim port of Thankful Eyes, the Gedit color scheme
Vim Script
2
star
35

ringo

the redis recipe framework
Ruby
2
star
36

wrappable

Generic module for a wrappable class
Ruby
2
star
37

tnetstrings-ruby

A ruby extension to parse and dump tnetstrings
C
2
star
38

cacher

A handy Ruby caching library with pluggable backends.
Ruby
2
star
39

stl-tulip-slides

Vim Script
2
star
40

lolgebrajs

In javascript, this time.
JavaScript
1
star
41

trello-skeletons-talk

Vim Script
1
star
42

synth

Ruby
1
star
43

smartbot

Scala
1
star
44

karel

the littlest robot
1
star
45

truth

Ruby
1
star
46

resume

my résumé
1
star
47

sokoban

an old try at a Ruby Quiz
Ruby
1
star
48

haskell-friends

my little "learning haskell" puzzle
Haskell
1
star
49

blog

a blag
JavaScript
1
star
50

ixl-rust

The ixl language, in rust this time.
Rust
1
star
51

jneen.net

My blag, based on jneen/blag
Ruby
1
star
52

plans

A clone of Grinnell Plans for general-purpose use
Ruby
1
star
53

gnome-terminal-ThankfulEyes

The Thankful Eyes theme for gnome-terminal
Shell
1
star
54

hilite.tex

shellout and syntax highlighting system for tex
TeX
1
star
55

better-schedule-search

Berkeley's schedule search sucks. We mashed it up and made it better. Now we're porting it to Sinatra.
Ruby
1
star
56

jayferd.github.com

JavaScript
1
star
57

core_ext

Things I Think Ruby Should Do For You
Ruby
1
star
58

money

a simple expense-sharing app
Ruby
1
star