JS.Class is a set of tools designed to make it easy to build robust object-oriented
programs in JavaScript. It’s based on Ruby, and gives you access to Ruby’s object,
module and class systems, some of its
reflection and metaprogramming facilities,
and a few of the packages from its standard library. It also provides a powerful
package manager to help load your applications as efficiently as possible.
It supports all the major web browsers and a growing number of server-side platforms,
including Rhino, Node.js and Narwhal. Wherever you’re using JavaScript, JS.Class
makes building large programs a snap.
Features
JS.Class provides many of Ruby’s powerful features, including:
- Ruby-like object system with classes, mixins, and singleton methods
- Late-binding arguments-optional
supercalls to parent classes and mixins included,extendedandinheritedhooks- Method binding
- Reflection APIs for the object system
- Conventions for object equality, comparison, iteration and hashing
- Versions of various standard Ruby modules and data structures
Its object system has been designed to exactly mimick the behaviour of Ruby, giving
you a great deal of power when structuring large programs. It also provides a set
of well-integrated data structures, all of which implement the powerful
Enumerable API. If you like Ruby, you’ll feel right at home.
Download version 2.1
Version 2.1 builds on the 2.0 core and adds several new core features and packages. A quick tour of the new features:
- New libraries:
ConstantScope,HashandHashSet, a much fasterSetimplementation. - The package manager has been improved with a new API, parallel downloading of files, and support for server-side environments such as SpiderMonkey, Rhino and V8. It also supports user-defined loader functions for transparent integration with Google and Yahoo!’s packaging systems.
- The
Enumerablemodule has been updated with plenty of methods from Ruby 1.9, and now supports enumerators, andSymbol#to_proc-like functionality whereby a string,MethodChainor any object that implementstoFunction()can be used as an iterator. - The core object methods now live in
Kernel, and we’ve added new methods:tap(),equals(),hash()andenumFor(). - The double inclusion problem is now fixed; no current Ruby implementation seems to support this properly.
- Ancestor and method lookups are now cached so
callSuperruns about twice as fast. - Classes can be named to generate
displayNameon methods for use with the WebKit debugger.
Most code based on version 1.x will be compatible with this release, but there a few issues to be aware of. If you have code you cannot update to 2.x at present, 1.6 is still available:
Acknowledgements
JS.Class was initially inspired by, and borrows ideas from, these open-source projects:
- The Prototype framework, © 2005-2010 Sam Stephenson
- Alex Arnell’s Inheritance library, © 2006, Alex Arnell
- Base, © 2006-9, Dean Edwards
The standard library has been influenced by techniques demonstrated in Pro JavaScript Design Patterns, and contains ports of a few standard Ruby modules.
JS.Class is © 2007–2010 James Coglan. You can find update announcements via
my blog. The source code is hosted on
Github.