Development Blog

Firebug Internals: Firebug’s private namespaces.

Wladimir Palant’s great post about using namespaces in XUL overlays gives me another chance to explain one aspect of Firebug source that throws some folks off at first.  All the Firebug source files have the following construct:

FBL.ns(function() { with (FBL) {const Cc = Components.classes;
const Ci = Components.interfaces;
...more constants...

Firebug.Debugger = ....

...more code...


What’s that stuff about? Well this code creates a giant private namespace for Firebug. The outer function FBL.ns() calls the anonymous function defined within it like this:


So the body of the file is run with ‘this‘ set to an empty and anonymous object, rather than the default, ‘window‘.  So, presto, no name conflicts like the one’s Wladmir points out.  We then can expose just the objects we want, mostly by attaching them to 'Firebug‘, an object defined in firebug.js as a property of the window ‘top‘.

The other thing going on is the  ‘with (FBL) {}‘, which injects all of lib.js into the namespace. Thus all of these utility functions can be called directly, just as if they were built in to the environment.

This namespace wrapper is a bit strange at first, but you quickly forget about it and concentrate on the code in the middle, secure in the knowledge that you are immune from the naming conflicts.


Comments are closed.