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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T01:15:07+00:00 2026-05-15T01:15:07+00:00

I follow a module pattern where I instantiate components, however, a lot of time

  • 0

I follow a module pattern where I instantiate components, however, a lot of time a component will only be instantiate one time (example: a comment system for an article).

For now I instantiate in the same JS file. but I was wondering if it is the wrong approach? It kind of make no sense to instantiate in the same file and always only once. But at the same time, if this file is in the page I want to have access to my module without instantiate from elsewhere, and IF I need another instance, I just create another from elsewhere…

Here is the pattern I follow:

  ApplicationNamespace.Classname = function() {
      // constructor    
      function privateFunctionInit() {
          // private
      }
      this.privilegedFunction = function() {
          // privileged
          privateFunction();
      };
   privateFunctionInit()
  };
  ApplicationNamespace.Classname.prototype = {
      Method: function(){}
  }
  var class = new ApplicationNamespace.Classname();

What do you think, wrong approach, or is this good?

  • 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-15T01:15:08+00:00Added an answer on May 15, 2026 at 1:15 am

    I don’t think there’s anything inherently wrong with this. We have a lot of JS classes in the codebase at my job that uses this pattern. Even though you’re essentially using the class as a singleton, structuring it this way allows you to create more instances in the future with ease, should you ever need to.

    In my own projects, I tend not to instantiate JS classes from within the file that defines them. I have a separate script that will instantiate whatever classes are needed on a page and work with them accordingly. That approach is probably more modular, but if you’re only ever using one instance, it’s mostly a matter of preference.

    If you’re sure you’re never going to use more than one instance of the class, you could simplify things by creating a singleton object with object literal notation.

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

Sidebar

Related Questions

I have been trying to follow the example at http://xacmlinfo.com/2011/12/18/writing-jdbc-pip-module/ in order to create
I follow this pattern to organize my js application. As that example says our
The project I'm working on requires a ticketing module, able to create, comment, follow,
I have a ruby file as follows: module Example class Myclass def t_st Hello
Follow up question from Multi-core usage, threads, thread-pools . Are threads moved from one
I'm trying to follow the custom module tutorial at http://dojotoolkit.org/book/dojo-book-0-9/part-3-programmatic-dijit-and-dojo/modules-and-namespaces/creating-your-own-modul I've got a local
I'm creating a module for the shopping cart PrestaShop so I have to follow
I am building a backend module (written in PHP) that will be used for
I have started learning the javascript module pattern and I have the following code:
I have define two record types as follow: (* in module A*) type reg

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.