Namspaces please

Last week I saw a forum post that tweaked my curiosity:

Incompatibility between SQLite Manager and Source Tree extensions

As it turns out, a subtle bug in Ivan’s sourcetree extension caused problems for the Sqlite extension because it accidentally re-defined by using ‘open’ as a local variable. This is a nasty gotcha when getting used to javaScript variable scoping:

function foo() {
open = 1;
alert( == open); // true!

function bar() {
var open = 2;
alert( == open); // false!

Essentially, unless you ‘var’ a variable to declare it in the current scope, that variable goes onto the top window object, and can cause all sorts of havoc. I remember a Komodo bug that cropped up just before a release that had the same issue related to a mis-scoped variable called ‘view’. A 1-line fix in both cases, but these sorts of fixes take some tracking down.

In Komodo we use JavaScript namespaces for almost all of our JS code, and if you are doing any amount of extension writing we strongly recommend you follow this practice as well. A namespace is like a nice little container that you can fill up with anything you want and carefully control what gets exported to the outside world:

/* this is the techno namespace. For house music, define everything under Chicago.*
* if you don't know what any of this is, go watch

if(typeof(techno) == 'undefined') {
var techno = {};

if(typeof(techno.minimal) == 'undefined') {
techno.minimal = {}; // we don't need var here, we're adding a property onto techno

// everything inside this closure is in our namespace
(function() {
// private variable
var samplerate = 44100;

// exported variables
this.bpm = 128;
this.acid = true;
this.detroit = true;

// exported function
this.clickySounds = function() {
return 'click click clicky';

// private function
function _get_loop_sample_length(bars) {
return (bars*4) * ((samplerate * 60) / this.bpm);
}).apply(techno.minimal); // apply the closure to the namespace

Using namespaces in your Komodo extension code will not only immunize your code from other people’s, it will immunize their code from yours as well. You can of course declare any namespace you want, but it might be more sensible for the community to standardize on something like:


Recent Posts

Scroll to Top