matt good

musing coding

Simple JavaScript Namespaces

written by Matt, on May 31, 2009 12:52:00 PM.

“Namespaces are one honking great idea — let's do more of those!” - Tim Peters, “The Zen of Python”

After a few years of mostly server-side development I'm getting back into JavaScript, and for the first time I feel like I'm approaching it as an actual engineering challenge rather than a bunch of quick hacks. As our JS code base is growing I'm breaking things up into namespaces to keep things manageable.

Though JavaScript has no built-in notion of "modules" like many other languages you can use objects to create namespaces, which in their simplest form would look something like:

util = {
  my_func: function() {
    alert('my_func');
  }
};
util.my_func();

However, creating nested namespaces like "myapp.util.text" gets a bit ugly with that approach. I liked the basic API of Namespace.js, though I didn't want any of the additional features like remote loading, so I figured I could make something much simpler and smaller.

You can first "declare" a namespace and then assign attributes like:

Namespace('myapp.ui');
myapp.ui.ToggleButton = function() {
  // ...
};

Or pass in the attributes to include in that namespace:

Namespace('myapp.util.text', {
  some_func: function() {
  }
});

Here's the code:

function Namespace(name, attributes) {
  var parts = name.split('.'),
      ns = window,
      i = 0;
  // find the deepest part of the namespace
  // that is already defined
  for(; i < parts.length && parts[i] in ns; i++)
    ns = ns[parts[i]];
  // initialize any remaining parts of the namespace
  for(; i < parts.length; i++)
    ns = ns[parts[i]] = {};
  // copy the attributes into the namespace
  for (var attr in attributes)
    ns[attr] = attributes[attr];
}

This is of course much smaller than the 16kb of Namespace.js, so as an additional challenge I decided see if I could compact it to fit the 140 character limit of a Twitter status message :) (Note: the Twitter version used jQuery's $.extend function to copy the attributes into the namespace which I later replaced with the framework-independent version above.)

I'll follow up later with some tricks I've been thinking about to "import" stuff from other namespaces.

Safari 3

written by Matt, on Aug 28, 2007 6:55:17 AM.

I think I've tried just about every web browser available for the Mac. The included Safari 2 didn't feel quite right, Firefox was too slow and a memory hog, Flock, Shiira and some others just didn't really fit what I was looking for. Camino was nice and light, and better integrated with the Mac than Firefox, but I was missing a good search box and it felt a little too limited in features. Now I'm using the Safari 3 beta and couldn't be happier. There are lots of little things that just feel "right" and it fits with the Mac OS nicely. After downloading an application you are warned that it contains an app, and if you approve the DMG file is mounted so you can run or install the app. But, the one thing that seems simple, but I've felt missing in browsers for a while is the tab management. On Linux I used Epiphany which has supported rearranging tabs for a while, and Firefox now supports it, but Safari gets one more thing right: dragging tabs between windows. So far I haven't seen another browser do this, but in Safari you can drag a tab up or down to detach it from the current window and either drag it to a different window, or out into a new window. It's not a feature I use constantly, but I do really like being able to keep my tabs organized in a logical manner instead of having a random assortment of tabs all open in the same window. Thanks Apple for getting this right.

Scripts: svndiff

written by Matt, on Aug 5, 2007 9:24:00 AM.

To make the output of the "svn diff" command more readable here's a small script to pipe the output to the Pygments library to colorize the command line output:

#!/bin/bash
svn diff "$@" | pygmentize -ldiff

Industrial Design

written by Matt, on May 2, 2007 5:14:27 AM.

For April 1st ThinkGeek presented us with this wonderful device:

Vilcus Plug Dactyloadapter

However, this product was created by another industrial design company that has a lot of extremely creative products, such as this phone with rotation aware clock display:

Mabbila Phone

Unfortunately many of their products are only produced in small runs, so availability is limited. However, it's worth browsing their industrial design catalog to see some of the cool ideas.

Mac Migration (part 1)

written by Matt, on May 1, 2007 6:42:25 AM.

A week ago I started my new job at YouTube. Most people here use Macs so I got a nice shiny new MacBook Pro to work on. I've been using Linux (Debian and Ubuntu) almost exclusively for about 4 years now and Windows before that, so I've been quickly getting up to speed on using my new Mac.

One of my first major annoyances was that some form controls weren't keyboard navigable. Filling out web forms was frustrating since hitting Tab would skip past drop-down fields, and when dialogs popped up and I didn't want to respond with the default button I had to switch over to the mouse instead of just tabbing to the right one.

Fortunately I found these instructions on changing this behavior. Now I can use the keyboard to quickly navigate these inputs.

More on my Mac switch to come.

PyCon Trac Presentation

written by Matt, on Feb 28, 2007 11:08:20 AM.

Here are the materials from my PyCon Trac presentation:

The modified rst2s5 script requires Pygments for coloring the example code.

PythongPaste

written by Matt, on Feb 27, 2007 12:51:31 AM.

Ian Bicking has just added a new package to the Paste suite for WSGI utilities

Read on...

Ubuntu package for Germanium

written by Matt, on Feb 3, 2007 4:17:08 PM.

I've built an Ubuntu Edgy package for Germanium. It may work on Dapper, or Debian versions, but I haven't tested it on any of those yet. I think the dependencies should be covered, but if you find any problems you can open a ticket.

Download:
germanium_0.2.0-0ubuntu1_all.deb

Germanium 0.2.0 Released

written by Matt, on Feb 2, 2007 7:07:00 PM.

Germanium 0.2.0 features better GNOME integration including mime handling for .emp files and a GConf schema, keyboard shortcuts, as well as album art display, and optionally saving album art with the tracks.

Download:
emusic-gnome-0.2.0.tar.gz

Darcs:

darcs get --tag=0.2.0 http://projects.matt-good.net/darcs/emusic-gnome/

Trac Nominated for Linux User Awards

written by Matt, on Oct 16, 2006 12:56:03 PM.

Trac has been nominated in the Linux User Awards for "Best Linux/OSS Developer Tool". Launchpad and Mono are the other nominations in this category, so we're among some pretty big competition. The Trac community has grown tremendously in the past year, so it's nice to see that it's so highly regarded.