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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:32:54+00:00 2026-05-26T01:32:54+00:00

Just wanted to know if it was a good JavaScript practice. Let’s say I

  • 0

Just wanted to know if it was a good JavaScript practice.

Let’s say I have many Web pages that all call an initialization function “init()”, would it be the right thing to use an IIFE inside my pattern to run the function everytime the script is loaded?

var foo = (function() {
    var bar = "something";

    (function init() {
        // Do something crazy that's gonna be the same across all my web pages
        // like adding an event listener or something
        // ...
        document.write('page init...');
    }());

    function privatePage1() {
        // This stuff is gonna be used only in page1.html via foo.privatePage1
        document.write('page 1' + bar);
    }

    function privatePage2() {
        // This stuff is gonna be used only in page2.html via foo.privatePage2
        document.write('page 2' + bar);
    }

    return {
        privatePage1: privatePage1,
        privatePage2: privatePage2
    }
}());
  • 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-26T01:32:55+00:00Added an answer on May 26, 2026 at 1:32 am

    This is a pretty subjective area, but here’s my take:

    • When you use the module pattern, you’re providing a contained set of functionality to the rest of your code. It’s essentially a mini-library.

    • In general, I wouldn’t expect a library to do anything when I load it, other than initialization steps that are entirely internal to the library (e.g. setting up the configuration, instantiating a few necessary objects, etc) – nothing that actually affects the DOM or otherwise significantly alters the environment (which is why I’ve never been entirely comfortable with libraries like Date.js or Prototype that change the prototypes of basic objects).

    • There are a couple of reasons for this, but the main one is that I don’t want to have to worry about the load order of my libraries/modules, other than simply managing dependencies. Independent modules shouldn’t affect each other at all. When you manipulate the DOM in your module at load time, sooner or later you’ll realize that another piece of your code is expecting the DOM to be in a certain state at a certain time, and that you now have to care about whether you load your module before or after that time. This is an extra bit of complexity that’s essentially hidden in the script tag that loads your module.

    • The other issue here is portability and adaptability. Maybe you’ll want to use your module in another project with another DOM setup. Maybe you’ll want to pass a different DOM element or config variable to the init() function on a specific page. If you execute init() automagically, you lose the opportunity for configuration.

    So what I generally do is to set the init() method as an attribute of the returned module object:

    var foo = (function() {
    
        function init() {
            // Do something crazy that's gonna be the same across all my web pages
        }
    
        //...
    
        return {
            init: init,
            // etc
        }
    }());
    

    and then call it as needed elsewhere in my code:

    foo.init();
    

    Yes, this adds an extra line of redundant code to the initialization for all my pages (though this is probably just one other script anyway, so the added weight is all of 11 characters). But it allows me a more fine-grained control over when the module is initialized, and offers a hook for configuration arguments when I (inevitably) determine I need them later.

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

Sidebar

Related Questions

PERL? Perl? perl? What's good style? I know the answer—I just wanted to make
I just wanted to know your experience with using resharper. We have a very
i implemented an application for iPhone, i just wanted to know that can i
Just wanted to know if it is possible to disallow the whole site for
Just wanted to know if anyone is really using Objects and Collections in Oracle
Just wanted to know if there is a big chance to inject SQL While
Just wanted to know if overriding UITabBarController would get my app rejected? Is it
I just wanted to know what's the difference between clear() and str(); For example:
I just wanted to know how to configure FCKEditor to upload files and images
I just wanted to know what is the main advantage of using the iterators

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.