Wednesday, March 19th, 2008

JavaScript Metaclass Programming

Category: JavaScript

<>p>Neil Roberts is continuing a really nice set of blog posts with one that teaches us about JavaScript Metaclass Programming:

Metaclass programming is the programming that builds a class. Since most of the time a metaclass simply takes an already declared class and manipulates it, you can think of a metaclass as a template: a class goes in, and a specifically structured class comes out.

Why didn’t I realize that I was using it in JavaScript? Well, mostly because if you want to build a class structure in JavaScript, you are forced to do everything related to OO in JavaScript programatically. Want to do inheritance? There’s no syntax for it, you have to do it programatically. Want to add some functions and properties? You have to do it programatically.

Which is why almost all of the toolkits make some provisions for doing these programatic procedures. All of these tools are related to metaclass programming, but they’re almost all geared to constructing a class. What metaclasses are truly awesome for is manipulating a class, and we’ll be doing that today.

This article reminds me of the talk that Glenn Vanderburg uses to give on JavaScript that went deep into the language and had a bunch of people a bit surprised at things they didn’t know about the language.

Neil walks us through a mixin that can make your data private, ending up at:

< view plain text >
  1. function privatize(instance){
  2.   var c = instance.constructor;
  3.   var p = c.prototype;
  4.   if(!c._privatize){
  5.     c._privatize = { privates: {}, functions: {} };
  6.     for(var key in p){
  7.       var value = p[key];
  8.       if(key.charAt(0) === "_"){
  9.         c._privatize.privates[key.slice(1)] = value;
  10.         delete p[key];
  11.       }else if(dojo.isFunction(value)){
  12.         c._privatize.functions[key] = true;
  13.       }
  14.     }
  15.   }
  17.   var context = dojo.delegate(instance, c._privatize.privates);
  18.   context.public = instance;
  20.   for(var fn in c._privatize.functions){
  21.     instance[fn] = dojo.hitch(context, instance[fn]);
  22.   }
  23. }

You can check out a demo here. Keep ‘em coming Neil!

Related Content:

  • DukeScript: Bridging Java to JavaScript
    Oracle's NetBeans Platform Architect Jaroslav Tulach discusses how DukeScript will bridge Java with JavaScript in this...
  • JavaScript Learning Guide
    This guide introduces you to JavaScript in a Notes/Domino environment, explains best practices and pitfalls to avoid and provides...
  • Diet Ajax, 100% JavaScript free
    The open source Ajax framework ZK 1.0 has been released with its own markup language along with XUL and XHTML components to get around programming in...
  • Scripting elevation in Windows Vista with JavaScript
    This scripting technique allows you to elevate a given process to Admin. It involves creating a script—written in JavaScript—that can be...
  • Chapter 22: JavaScript security
    JavaScript continues to find adherents. But this scripting language can be used by malicious hacks to eat up memory .. and worse. Learn about Java...

Posted by Dion Almaer at 6:54 am

3.6 rating from 16 votes


Comments feed TrackBack URI

More interesting than yesterday :D
But, maybe someone could be interested in how to inject protected methods too?

Comment by Andrea Giammarchi — March 19, 2008

We provide just about the same thing in Archetype Components (see ) ;)

@Andrea your link is very interesting ! :)

Comment by temsa — March 19, 2008

Leave a comment

You must be logged in to post a comment.