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

  • Home
  • SEARCH
  • 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 8341225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:24:56+00:00 2026-06-09T05:24:56+00:00

I’m looking to encapsulate my javascript inside a namespace like this: MySpace = {

  • 0

I’m looking to encapsulate my javascript inside a namespace like this:

MySpace = {

   SomeGlobal : 1,
   A: function () { ... }, 
   B: function () { ....; MySpace.A(); .... },
   C: function () { MySpace.SomeGlobal = 2;.... }
}

Now imagine that instead of a few lines of code, I have about 12K lines of javascript with hundreds of functions and about 60 globals. I already know how to convert my code into a namespace but I’m wondering if there’s a quicker way of doing it than going down 12K lines of code and adding MySpace. all over the place.

Please let me know if there’s a faster way of doing this.
Thanks for your suggestions.

  • 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-06-09T05:24:59+00:00Added an answer on June 9, 2026 at 5:24 am

    I like to wrap up the namespace like so. The flexibility is huge, and we can even separate different modules of the MySpace namespace in separate wrappers if we wanted too. You will still have to add some sort of _self. reference infront of everything, but at least this way you can change the entire name of the namespace very quickly if need be.

    You can see how with this method you can even call _self.anotherFunc() from the 1st module, and you’ll get to the second one.

    (function (MySpace, $, undefined) {
    
        var _self = MySpace; // create a self-reference
        _self.test = function () { 
            alert('we got here!'); 
            _self.anotherFunc(); // testing to see if we can get the 2nd module
        };
    
        _self = MySpace; // reassign everything just incase
    
    }(window.MySpace = window.MySpace || {}, jQuery));
    
    $(function () { 
    
        MySpace.test(); // call module 1
        MySpace.callOtherModule(); // call module 2
    
    });
    
    // Here we will create a seperate Module to the MySpace namespace
    (function (MySpace, $, undefined) {
        var _self = MySpace; // create a self-reference
    
        _self.callOtherModule = function () {
            alert('we called the 2nd module!');    
        };
    
        _self.anotherFunc = function () { 
            alert('We got to anotherFunc from the first module, even by using _self.anotherFunc()!'); 
        };
        _self = MySpace; // reassign everything just incase
    
    }(window.MySpace = window.MySpace || {}, jQuery));​
    

    jsFiddle DEMO

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a jquery bug and I've been looking for hours now, I can't
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
I am trying to render a haml file in a javascript response like so:
I need a function that will clean a strings' special characters. I do NOT
I know there's a lot of other questions out there that deal with this
I would like to count the length of a string with PHP. The string

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.