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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T03:13:23+00:00 2026-05-15T03:13:23+00:00

I have a pretty big Javascript script with loads of global variables & functions

  • 0

I have a pretty big Javascript script with loads of global variables & functions in it. Then a piece of code that calls one function from this js file: myfunc();

Ok, now I have cloned this script and modified some functionality, all function prototypes and variables are named the same in both scripts. So now I have two scripts loaded and one call to myfunc(), now we have a clash because there are loads of global variables with the same names and two myfunc()s.

What I want to do is wrap this cloned script in a namespace, so that I can modify the original call to: clone.myfunc() which will call the new function, but I also want myfunc() to just refer to the original script. In other words I can’t touch the original script (no permissions) and I want to be able to use both the clone and the original at runtime.

This is the script im cloning: http://pastebin.com/6KR5T3Ah

Javascript namespaces seem quite tricky this seems a nice namespace method:

var namespace = {
    foo: function() {
    }

    bar: function() {
    }
}

...

namespace.foo();
}

However that requires using an object, and the script (as posted above) is humongous at nearly 4000 lines, too much to objectize I think?

Anyone know a better solution to avoid namespace pollution, with one script I cant touch and one being a clone of that script. Just so I can call myfunc() and clone.myfunc() and all global variables will behave in their respected scope.

It’s either that, or I go through and modify everything to have unique names, which may take a lifetime

This is a Mozilla addon if it helps context wise.

Thanks.

  • 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-15T03:13:24+00:00Added an answer on May 15, 2026 at 3:13 am

    In general, you can avoid polluting the global namespace really easily. If you have code that looks like this:

    var foo;
    
    function bar() {
    }
    

    just wrap it in a function and immediately call the function:

    (function() {
        var foo;
    
        function bar() {
        }
    })();
    

    Now foo and bar are constrained to that big anonymous function. They can still refer to each other with unqualified names, but they’re no longer “global” properties (properties on the window object). This is sometimes called wrapping things up in a closure.

    You can “export” things out of that closure in a few ways: 1. Return an object with function properties; 2. If you only need to export one function, just return the function reference; 3. Explicitly assign functions to window properties from within the closure:

    // 1. Returning an object with function properties:
    var myNamespace = (function() {
        function myfunc() {
            // ...
        }
    
        return {
            myfunc: myfunc
        };
    })();
    

    The call would be to myNameSpace.myfunc().

    // 2. Returning just one function
    var myFuncReworked = (function() {
        function myfunc() {
            // ...
        }
    
        return myfunc;
    })();
    

    The call would be to myFuncReworked().

    // 3. Explicitly assign to `window` property:
    (function() {
        function myfunc() {
            // ...
        }
    
        window.myfunc = myfunc;
    })();
    

    The call would be to myfunc() (aka window.myfunc()).

    All of this assumes that the script doesn’t assume that its various global variables and functions will show up on the window object, of course.

    This blog post may be useful in terms of background around the whole “wrapped in a closure” thing, although the thrust of the post is about avoiding anonymous functions (except scoping functions).


    Edit Pointy points out (and that makes sense) that this is a plug-in, so if window isn’t relevant, that third option can look like this instead:

    // 3. Explicitly assign to global property:
    (function(global) {           <== Note the new argument
        function myfunc() {
            // ...
        }
    
        global.myfunc = myfunc;   <== Use the new arg instead of `window`
    })(this);                     <== Pass `this` into the function
    

    That works because at global scope, this refers to the global object, which doesn’t intrinsically have a “name;” passing it into the function lets us give it one easily. (In web pages, window refers to the global object, but actually window is just a property on the global object that it uses to refer to itself; the technical discussion of that gets…technical fairly quickly, so I’ll leave it as I don’t think it’s hyper-relevant.)

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

Sidebar

Ask A Question

Stats

  • Questions 453k
  • Answers 453k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer That's not UTF-8. That's quoted printable, which quite isn't the… May 15, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer I had a line in my touchesBegan method that set… May 15, 2026 at 9:23 pm
  • Editorial Team
    Editorial Team added an answer When you create new queries, and use them for the… May 15, 2026 at 9:23 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.