Refining Superclass Method Calls in JavaScript

February 10, 2007

Originally posted to ydnar.vox.com in February 2007.

Last week I was revisiting the always fun problem of implementing “classical” inheritance in JavaScript. I’d taken a few stabs at it, and had gotten it to a reasonably good state that borrowed some good ideas from Doug Crockford, Sam Stephenson, and Dean Edwards. Joshua Gertzen wrote a good post about various methods on his blog.

I’ve never been terribly thrilled with the form Class.superClass.method.apply( this, arguments ). It was redundant: replicating both the class and method names. Copy & paste of code could lead to subtle errors, and it’s annoying to type that much. But the alternatives were worse: Recompiling the function to generate a “magic” lexical for the superclass or wrapper methods. So the Class object basically sat untouched for a year and a half.

Back to last week…It occurred to me that in all the JavaScript we’d built for Vox, we almost never shared a method between two objects, except via inheritance. There were a couple exceptions, but they could be rewritten (it turned out to be a good idea anyway). Second, functions are objects like everything else, and can have arbitrary properties. Third, arguments.callee is available in every function call in JavaScript. I realized then that storing the superclass was not as useful as just storing the supermethod.

For any given method in a class, store its supermethod as a property of the method: method.__super. Instead of the unwieldy construct above, any method could simply use arguments.callee.__super.apply( this, arguments ).

The Class constructor from Core.js:

Class = function( sc ) {
var c = function( s ) {
this.constructor = arguments.callee;
if( s === __SUBCLASS__ )
this.init.apply( this, arguments );

c.override( Class );
sc = sc || Object;
c.override( sc );
c.__super = sc;
c.superClass = sc.prototype;

c.prototype = sc === Object ? new sc() : new sc( __SUBCLASS__ );
c.prototype.extend( Class.prototype );
var a = arguments;
for( var i = 1; i < a.length; i++ )
c.prototype.override( a[ i ] );

for( var p in c.prototype ) {
var m = c.prototype[ p ];
if( typeof m != "function" || defined( m.__super ) )
m.__super = null;
var pr = sc.prototype;
while( pr ) {
if( defined( pr[ p ] ) ) {
m.__super = pr[ p ];
if( pr === pr.constructor.prototype )
pr = pr.constructor.prototype;

return c;

arguments.callee was useful in the constructor too: Instead of creating a circular reference by overriding the constructor like this: constructor.prototype.constructor = constructor, the constructor itself can just set it on the this object when the constructor is called: this.constructor = arguments.callee.

Calling a supermethod can be simplified further, to arguments.callee.applySuper( this, arguments ) via a little sugar:

Function.prototype.extend( {
applySuper: function( o, args ) {
return this.__super.apply( o, args );

callSuper: function( o ) {
var args = [];
for( var i = 1; i < arguments.length; i++ )
args.push( arguments[ i ] );
return this.__super.apply( o, args );
} );

At the Symphony

February 8, 2007

Originally posted to ydnar.vox.com in February 2007.

Steve Jobs on DRM

February 7, 2007

Originally posted to ydnar.vox.com in February 2007.

Interesting letter from The Steve:

The third alternative is to abolish DRMs entirely. Imagine a world where every online store sells DRM-free music encoded in open licensable formats. In such a world, any player can play music purchased from any store, and any store can sell music which is playable on all players. This is clearly the best alternative for consumers, and Apple would embrace it in a heartbeat. If the big four music companies would license Apple their music without the requirement that it be protected with a DRM, we would switch to selling only DRM-free music on our iTunes store. Every iPod ever made will play this DRM-free music.

Why would the big four music companies agree to let Apple and others distribute their music without using DRM systems to protect it? The simplest answer is because DRMs haven’t worked, and may never work, to halt music piracy. Though the big four music companies require that all their music sold online be protected with DRMs, these same music companies continue to sell billions of CDs a year which contain completely unprotected music. That’s right! No DRM system was ever developed for the CD, so all the music distributed on CDs can be easily uploaded to the Internet, then (illegally) downloaded and played on any computer or player.

In 2006, under 2 billion DRM-protected songs were sold worldwide by online stores, while over 20 billion songs were sold completely DRM-free  and unprotected on CDs by the music companies themselves. The music companies sell the vast majority of their music DRM-free, and show no signs of changing this behavior, since the overwhelming majority of their revenues depend on selling CDs which must play in CD players that support no DRM system.

So if the music companies are selling over 90 percent of their music DRM-free, what benefits do they get from selling the remaining small percentage of their music encumbered with a DRM system? There appear to be none. If anything, the technical expertise and overhead required to create, operate and update a DRM system has limited the number of participants selling DRM protected music. If such requirements were removed, the music industry might experience an influx of new companies willing to invest in innovative new stores and players. This can only be seen as a positive by the music companies.

Pigs Fly

February 7, 2007

Originally posted to ydnar.vox.com in February 2007.

First Jobs denounces DRM and then Gates announces support for OpenID?


Perian Codec for QuickTime

February 7, 2007

Originally posted to ydnar.vox.com in February 2007.

Just ran across this little gem: Perian, an open-source QuickTime codec for a few of those other formats that QT chokes on.

Perian enables QuickTime® application support for additional media types including:

  • AVI and FLV
  • 3ivX, DivX, Flash Screen Video, MS-MPEG4, Sorenson H.263, Truemotion VP6, and Xvid
  • AVI support for: AAC, AC3 Audio using A52Codec, H.264, MPEG4, and VBR MP3


Brad Gets Twenty Seven

February 7, 2007

Originally posted to ydnar.vox.com in February 2007.

Fun list:

  • Sazeracs
  • Cuties
  • The loft room with the orange lighting


Monday Blue Bottle

February 5, 2007

Originally posted to ydnar.vox.com in February 2007.

East Bay Riding

February 5, 2007

Originally posted to ydnar.vox.com in February 2007.

Tina and I rode 160 miles this weekend. 75 on Saturday, taking a modified Mt. Tam, Seven Sisters + Alpine Dam route, out through Fairfax with a Strawberry detour. 85 on Sunday, on this brutal fun Luna Chix training route that took us to the top of every hill in Oakland, some twice.

High point: Riding on near-abandoned east bay roads on Super Bowl Sunday.

Low point: Missing Prince’s halftime show by 5 minutes.

I went to bed at 9. Pista was happy about that…

Notes on the Morning of the 1st

February 2, 2007

Originally posted to ydnar.vox.com in February 2007.

The first of the month, when there are bills to pay and other sundry tasks to get one’s house in order. Rent (or mortgage), car insurance, gym memberships, PG&E. This February brings another pair of open hands: Renter’s insurance and the ever-looming car registration tax. I suppose I should be grateful they are as small an amount as they are. Renter’s insurance is almost laughably cheap, and I have been blessed with living in an area of SOMA designated as a special residential parking zone.

I pay $60 per year for a sticker on the right-rear bumper that lets me park with impunity in any non-metered space in my district. I’m not fully taking advantage of this the way that Danielle Steele is, but I do appreciate the way that our office also happens to fall within the oddly gerrymandered area that is District U.

As it happened, the first of February fell on a Thursday this year, which means I had to park somewhere other than my lovely alley due to San Francisco’s brilliant parking revenue recovery street cleaning program. Returning home last night I dropped my car off around the corner in a metered spot, pulled various valuables out (cars are broken into in my neighborhood fairly often) and made a mental note to move it in the morning.

Which brings us back to this morning, the first of February. I left my door at approximately 8:02 AM, Pista in tow (or was it the other way around) and walked around the block to move my car. In the intervening time, Pista decides to go for the gold, filling up a pair of, well, you get the idea. Her deposits deposited in the trash, we walk the remaining half-block to my car.

Where there it was, in all its glory: A DPT envelope, fluttering in the wind beneath my car’s windshield wiper. Reaching for the envelope, my blood pressure rising, I made a mental run through the list of reasons I could have potentially gotten a ticket:

  • It’s February. My registration expires this month. Does it expire on the 1st or the 28th?
  • The meter starts running at 8 AM and not 9 AM.
  • The DPT agent decided to fuck with me.

In the end it turns out to be none of the above. Maybe a bit of the last, but the reason for my minutes-old parking citation: Parking on a hill without turning the front wheels. Not only did I get a ticket between the time I left my front door and arrived at my car (8:04 AM), or on a day I specifically moved my car to avoid getting a ticket, or parking exactly between the proscribed white lines—but I got a ticket for the most utterly bullshit reason. I couldn’t even chalk it up to DPT quotas, unless my particular agent decided to get a jump on the short month ahead.

I shake my head, swearing and wondering if this was the sort of thing that led to the recent spate of violence against DPT officers.

So another ticket to throw on the pile, another 35 bucks between me and a new yearly registration for the Subaru.

Footnote: I heard a curious thing: A car waiting at the light, windows cracked, blasting Sufjan like it was 50 cent. Trumpets don’t quite vibrate the license plate holder the way that dub does, but the emo-indie-christian mob made me laugh.

Vox Hunt: So Meta

January 31, 2007

Originally posted to ydnar.vox.com in January 2007.

Show us a photo of someone else taking a photo.
Submitted by ydnar.

summer grillmeta metameta meta metameta meta meta meta