#578 ✓incomplete
Lea Hayes

Possible extension idea: $namespace

Reported by Lea Hayes | March 4th, 2009 @ 01:52 AM

Hi all,

A recent project of mine involved a lot of JavaScript, so naturally there were a lot of namespaces. We found that we kept running into two problems when using namespaces:

  1. Sometimes a namespace was not available and would result in an error:

First.Second.Third = { ... };

Resolving this issue caused some pretty ugly code:

if (!First)
   First = {};
if (!First.Second)
   Second = {};
if (!First.Second.Third)
   Third = {};
  1. Sometimes we had scripts which were redefining the entire contents of a namespace.

So, I came up with the method $namespace which aims to ease the above two issues:

The following two examples of usage defines/redefines values:

$namespace('First.Second').Third = {
   foo: function() {

$namespace('First.Second.Third').foo2 = function() {
  // Function could be located in another JS file/script...

The following example shows how to add to a namespace without redefining the entire thing:

$namespace('First.Second.Third', {
   foo: function() {

$namespace('First.Second.Third', {
   foo2: function() {
      // Function could be located in another JS file/script...

Here is the function implementation that I use:

var $global = window;

function $namespace(name, extension) {
    var namespaces = name.split('.');
    var parentNS = $global;
    namespaces.each(function(nsID) {
        var ns = parentNS[nsID];
        if (ns === undefined || ns === null)
            parentNS[nsID] = (ns = {});
        parentNS = ns;
    if (extension) // Extend namespace?
        Object.extend(parentNS, extension);
    return parentNS;

I thought that this could be of use to a lot of developers.

Many thanks, Lea Hayes

Comments and changes to this ticket

  • Tobie Langel

    Tobie Langel March 4th, 2009 @ 06:14 PM

    • State changed from “new” to “incomplete”

    Thanks for your suggestion. Due to the large number of suggestions we receive, we can only accept enhancement requests with tested patches in the form of a diff file or (preferably) a GitHub pull request.

    For help with creating a tested patch, see our contribute page.

    Please feel free to reopen this ticket once you have either of these ready.

    If you feel like discussing your enhancement proposal before working on a patch please join the conversation on the core mailing list.

    Thanks for taking the time to contribute!

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:07 AM

    • Tag changed from enhancement, extension, feature, function, prototype to prototype, section:lang

    [not-tagged:"function" tagged:"section:lang" bulk edit command]

  • Tobie Langel

    Tobie Langel July 24th, 2009 @ 02:34 AM

    • Tag changed from prototype, section:lang to section:lang

    [not-tagged:"prototype" bulk edit command]

  • Lea Hayes

    Lea Hayes January 9th, 2011 @ 02:57 PM

    • Assigned user set to “Tobie Langel”
    • Importance changed from “” to “”

    First off, I wish you guys a happy new year!

    Apologies, but it has been a while since I have looked at this ticket. Around the same time that I wrote this there was a family emergency and I didn't get a chance.

    I am a little confused, how would I go about submitting this via a GitHub pull request? I have created a GitHub account. How do I find your page, and how do I create a test patch?

    I would very much like to submit this idea for use, but I just don't know how. I have found this to be very useful in my own work, and a couple of people have commented well on it. So I believe that a lot of other people may also benefit from this.

    Would it be possible for you to do this on my behalf?

  • Tobie Langel

    Tobie Langel January 9th, 2011 @ 03:52 PM

    As stated above, we receive a lot of requests and have limited resources, so I'm afraid I won't be able to assist you. I suggest you seek help from the mailing list. That said, given the little activity on this ticket, I don't believe there is much interest in seeing something like this be part of core at the moment.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins