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 6595851
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:59:37+00:00 2026-05-25T17:59:37+00:00

I want to keep my scripts organized in one .js file for all my

  • 0

I want to keep my scripts organized in one .js file for all my site (I have a mess right now), something like namespaces and classes in C#…

(function ($) {

    //private variables
    $.divref = $("#divReference");

    //Namespaces
    window.MySite = {};
    window.MySite.Home = {};
    window.MySite.Contact = {};

    //Public function / method
    window.MySite.Home.Init = function(params){
        alert("Init");

        MySite.Home.PrivateFunction();

        $.divref.click(function(){
            alert("click");
        });
    };

    //private function / method
    MySite.Home.PrivateFunction = function(){
        alert("Private");
    };

})(jQuery); 

Is this an idiomatic layout in jQuery and JScript?

  • 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-25T17:59:38+00:00Added an answer on May 25, 2026 at 5:59 pm

    This is more how I would implement the pattern you are trying to do:

    // MySite.Home Extension
    window.MySite = 
        (function ($, root) {
    
            //private variables
            var $divref = $("#divReference");
    
            //private function / method
            var privateFunction = function(){
                alert("Private");
            };        
    
            root.Home = {};
    
            // Public variable
            root.Home.prop = "Click"
    
            //Public function / method
            root.Home.Init = function(params){
                alert("Init");
    
                private();
    
                $divref.click(function(){
                    alert(root.Home.prop);
                });
            };
    
            return root;
    
        })(jQuery, window.MySite || {} );   
    
    // MySite.Contact Extension
    window.MySite = 
        (function ($, root) {
    
            root.Contact = {};
    
            // More stuff for contact
    
            return root;
    
        })(jQuery, window.MySite || {} ); 
    

    The first change is splitting each “namespace” into its own Module pattern, so private variables wont bleed from namespace to namespace (if you do intend them to be private to the namespace, which would be more C# esque). Second is rather than accessing window.MySite, pass in the object that you want to extend (in this case I’m calling it root). This will give you some flexibility.

    Your private methods weren’t really private. To make a private method, you just want to make a function var that it bound in the closure, but not assigned to a property on the externally visible object. Lastly, you probably don’t want to use $.somegarbage. Like mentioned in a comment, you are adding a property to the $ object, which will still be there when the closure is done. If you wanted something close, I would just use $somegarbage which some people seem to like to do, but any variable name will work for private variables, just as long as the variable is bound in the closures scope (not somewhere else)

    You are on the right track…

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

Sidebar

Related Questions

I have a Bash script where I want to keep quotes in the arguments
I want to keep only numbers and remove all characters from a variable. For
I have many elements on page and all of which i want to translate
What is the best way to keep large projects organized with SSMS? I want
I have a web application where I want to keep track of the number
I have a config.js file that has all my app settings inside it. Some
I want to keep logs of some things that people do in my app,
I want to keep my website/s in version control (Subversion specifically) and use svn
I want to keep the class simple and not defined a constructor so i
I want to keep viewstate of a specific page in session but following code

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.