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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:25:48+00:00 2026-06-04T02:25:48+00:00

I somewhat new to Javascript and I’m stuck on this one item. Can someone

  • 0

I somewhat new to Javascript and I’m stuck on this one item. Can someone please show me how to make a javascript variable only usable on the .js file that it is on.

EXAMPLE:

Say I have two .js files, PG1 & PG2.
PG1 contains var channel = Channel01;

PG2 contains a variable with the same name, but a different entered Variable
(the part after the equals sign) (var channel = Channel02)

I don’t want function1 on PG1 to use the variable on PG2 or for function2 on PG2 to use the variable on PG1.

I am also calling these functions from a seperate HTML page.

Problem: All of my functions end up using only one variable no matter what page they are on.
(ex. function1 & function2 both use var channel = channel01)

Question: How can I limit functions on page1 to use only the variables on that .js page

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-06-04T02:25:49+00:00Added an answer on June 4, 2026 at 2:25 am

    module pattern :

    var myModule = (function(exports){
    
        var x = "foo";
    
        exports.myFunction = function(){
            alert(x);
        };
    
    })(typeof exports!="undefined" && exports instanceof Object ? exports : window );
    

    myFunction will be available in the window scope in the browser.

    EDIT

    i quote the author : “I am also calling these functions from a seperate HTML page.”

    so the author needs a global access to the functions he defines.

    var myModule is not needed though , nor export , it is just for AMD compatibility :

    (function(exports){
    
        var x = "foo";
    
        exports.myFunction = function(){
            alert(x);
        };
    
    })(window);
    

    now x for myFunction only exists in the closure , but myFunction can still access x in the global scope. any x definition in the global scope or whatever scope will not affect x = "foo"

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

Sidebar

Related Questions

I'm somewhat new to Javascript and I can't seem to figure out exactly what's
I think that Im missing some pieces for this. Im somewhat new to JavaScript
I'm extremely new to using jQuery/JavaScript, so please don't judge me if this is
I'm somewhat new to object oriented programming in Javascript and I'm trying to build
I am somewhat new to R, and i have this piece of code which
I am somewhat new to JavaScript, and have been having an issue with a
I'm somewhat new to JavaScript/jQuery, but when I saw examples of method chaining it
I have the following javascript object, somewhat pseudocode: { dateField: new Date(), addMinutes:function(numMinutes) {
I'm somewhat new to Javascript, not HTML/CSS or PHP/MySQL, just JS. I have a
I'm somewhat new to jQuery/javascript, and would like to compare the contents of row[i]

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.