Scope private classes in a separate file

Is there a way to create a private class in a separate file without polluting the global namespace? Currently I just create a sub-namespace and put all of the private classes that other public classes need to function, but is there a way to just make a class altogether private, yet in another file?

Answers


You are probably looking for the "Module Pattern" in JavaScript. The Module Pattern name can refer to a lot of different patterns but the basic concept to to declare functions and attributes that are only available to a function which has already been called. Like this:

 myModule = function () {
    var me = {},
        iAmPrivate = 1;

     function privateFunc() {
        // stuff can access iAmPrivate and iamPublic
     }

     me.iamPublic = 1;
     me.publicFunc = function () {
        // stuff can access iAmPrivate and iamPublic
     };

     return me;

 }());

 myModule.publicFunc();  // this can be called
 myModule.iamPublic;     // this can be accessed

 myModule.iAmPrivate     // xx can't do this
 myModule.privateFunc()  // or this

Because of how closures and scope works, Items declared local to that function call are available to all functions declared in that function call but nothing outside of the function call. The function has been called so they can never be accessed.

You can read a lot more about it here : http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html, and of course with a Google search now that you know what it is called.


Need Your Help

How to make a Silverlight page crawlable?

silverlight seo

With Navigation Framework, Silverlight becomes better suited for content pages. The next big thing would be to make it crawlable. I think one of the approaches could be abstracting the content in a

Can I prevent roaming of PasswordCredential stored in PasswordVault?

c# windows-store-apps credentials roaming passwordvault

In my application, I'd like to store the user credentials using the PasswordVault class, since it's the recommended way to do it. However, I realized that the credentials stored with this method are

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.