Wednesday, 4 April 2012

Announcing Lineage

Note: As of late 2015, I don't use Lineage anymore, there's no need. I use the class features of ES2015 (aka "ES6") instead. If my target is a browser, for now I transpile with Babel (no need when working in NodeJS, the latest Node uses the latest V8 which has solid support for ES2015).

Just a brief post to announce my latest mini-project, Lineage. It's a small, simple toolkit for creating JavaScript constructor functions and their prototypes ("classes," if you will) in a straight-forward and concise way. From the project home page:

  • Lineage's API lets you define prototypes with a very concise syntax, while still encouraging you to create functions with real names (rather than anonymous functions); this helps your tools help you (debuggers show function names in call stacks, for example).
  • Lineage provides a highly efficient mechanism for "supercalls" (calling into the parent prototype's versions of methods from an instance using a derived prototype).
  • Lineage's API encourages and supports use of the module pattern for each constructor and its prototype.
  • Lineage is small, <3k compressed (gzips to <1,500 bytes, a quarter of which is the MIT license) — because it doesn't try to reinvent inheritance, it just simplifies access to the power of JavaScript's prototypical inheritance.

Here's an example of defining a constructor called Thing with a spiffy function on the prototype:

var Thing = Lineage.define(function(p) {
p.spiffy = function() {
console.log("I'm a spiffy thing!");

...or if like me you prefer your functions to have names:

var Thing = Lineage.define("Thing", function(p) {
p.spiffy = Thing_spiffy;
function Thing_spiffy() {
console.log("I'm a spiffy thing!");

Now, with such a trivial example, that doesn't offer you much on top of the raw equivalent:

var Thing = (function() {
function Thing() {
Thing.prototype.spiffy = Thing_spiffy;
function Thing_spiffy() {
console.log("I'm a spiffy thing!");
return Thing;

...and that's the point, Lineage works with JavaScript's natural inheritance, it doesn't try to reinvent things. But when you get into inheritance, and in particular start making supercalls, Lineage reduces the code markedly while ensuring everything is hooked up properly, and does so in a way that's easy to use correctly, without retyping a lot of boilerplate code.

Rather than loading up this post with code examples, I'll point you to the progressive series of examples on the Lineage Comparison with Plain JavaScript page for more.

I'm using Lineage in projects now, and so far I'm really liking the simplicity of it. I hope you will too! If you do play with it, please send comments (positive and negative!), the feedback is very welcome.

Happy coding!


Fumisky Wells said...

Dear Crowder,

I am looking for OO library of Javascript which can work with jQuery (prototype.js might be a good choice, but a little verbose to avoid conflict of $-notation) so that I reach here.

I downloaded Lineage 0.4.1 and tried to use in node.js, but I have no idea how to do that?

I very appreciate some example code how to import/require Lineage in Node.js and/or browser side.

Best Regards,

Fumisky Wells.

T.J. Crowder said...

@Fumisky: I wouldn't use any library for this these days, I'd use the new `class` feature of ES2015 (aka "ES6"). (BTW, you've said you want an OO library "to use with jquery" but then later you talk about NodeJS. Normally jQuery is used on the client, in the browser, whereas NodeJS runs on workstations and servers, not browsers.)

Fumisky Wells said...

Thank you for your prompt reply.

Thank you for your information. ES6 'class' feature looks has not yet been implemented for IE11 and SF6 which are mainly used by our customer. So far some OO library is still necessary for these 2-3 years.

> you've said you want an OO library "to use with jquery" but then later you talk about NodeJS.

Sorry for confusing you. Main purpose of OO is for browser side application. What I meant node.js was just testing since using it on console(or terminal) is much easier than on browser for me.

T.J. Crowder said...

@Fumisky: It's fine that IE11 and Safari are behind, for now, using ES2015 for browser code means transpiling, which is what I do. I use Babel, but there are other options as well.

Fumisky Wells said...

Oh, ES2015 + Transpilling !! Now I understand!

Thank you for your information!