Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:10:56+00:00 2026-05-28T05:10:56+00:00

Are there any dangers/caveats one should be aware of when creating JavaScript namespaces? Our

  • 0

Are there any dangers/caveats one should be aware of when creating JavaScript namespaces?

Our project is fairly expansive and we are running a lot of JavaScript files (20+, expecting more). It is impossible to have any code maintainability without using namespaces, so we are implementing them like so:

var namespace1 = {

  doSomething: function() {
    ...
  },

  doSomethingElse: function() {
    ...
  }

}

And then to create hierarchies, we link them like so:

var globalNamespace = {
  functions1: namespace1,
  functions2: namespace2,
  ...

}

This works fine, but it is essentially a “trick” to make JS behave as if it did have namespaces. Although this method gets used a lot, most literature on this seems to focus on how to do it, and not whether there are any possible drawbacks. As we write more JS code, this is quickly becoming an integral part of the way our system works. So it’s important that it works seamlessly.

Were there any situations in which this “induced” namespace system caused you errors, or otherwise needed special attention? Can we safely expect identical behaviour across all browsers?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:10:57+00:00Added an answer on May 28, 2026 at 5:10 am

    The way you define namespaces in your example it appears to create globals out of each namespace so you end up with

    window.namespace1
    window.namespace2
    window.globalNamespace
    window.globalNamespace.namespace1
    window.globalNamespace.namespace2
    

    So if you have anything that clobbers window.namespace1 it will also clobber window.globalNamespace.namespace1

    edit:

    Here’s how we got around this problem:

    namespacing = {
        init: function(namespace) {
            var spaces = []; 
            namespace.split('.').each(function(space) {
                var curSpace = window,
                    i;  
    
                spaces.push(space);
                for (i = 0; i < spaces.length; i++) {
                    if (typeof curSpace[spaces[i]] === 'undefined') {
                        curSpace[spaces[i]] = {}; 
                    }   
                    curSpace = curSpace[spaces[i]];
                }   
            });
        }
    };
    

    Then you use it like this:

    namespacing.init('globalNamespace.namespace1');
    
    globalNamespace.namespace1.doSomething = function() { ... };
    

    This way you don’t have to introduce new global variables and you can confidently add to an existing namespace without clobbering other objects in it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Are there any dangers or security risks in allowing user css? Sorry for unspecific
Is there any danger in creating a linked server? I want to create a
Is there any danger running /etc/init.d/postgresql restart ?? We just had an incident where
When should I use Marshal.FinalReleaseComObject vs Marshal.ReleaseComObject ? Is there any danger in using
is there any danger if the rails html_escape function would stop escaping '&'? I
Is there any way to check whether a file is locked without using a
Is there any free or commercial component written in .NET (no COM interop) that
Is there any query which can return me the number of revisions made to
Is there any efficiency difference in an explicit vs implicit inner join? For example:
Is there any way to capture the MouseDown even from the .NET 2.0 TextBox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.