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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:29:18+00:00 2026-06-18T09:29:18+00:00

I have a javascript file named js/jsClasses.js which is relative to to the index.html

  • 0

I have a javascript file named js/jsClasses.js which is relative to to the index.html file.

This file has some nested functions. Here’s the code –

function JSClass(){ //notice the capital J which though doesn't change execution is a convention indicating a class constructor
var langSetting;
this.setLanguage = function(){
    langSetting = "en-GB";
    console.log(langSetting);
}
this.getLanguage = function(){
    return langSetting;
}

}

I need to set the value of the variable langSetting in index.html. This will be the global variable and in all the subsequent pages, I need to access the value of langSetting and load the page in the corresponding language.

At the moment, this is how I am trying to access the js/jsClasses.js file from index.html

<html>
    <script src="js/jsClass.js">
        var object1 = new JSClass();
        object1.setLanguage();
        object1.getLanguage();
    </script>



</html>

When I click the button, nothing happens. Can someone please help ?

  • 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-18T09:29:20+00:00Added an answer on June 18, 2026 at 9:29 am

    There are two ways to solve this, a bad and common one and a good one,

    The bad one is to remove the var key word from langSetting which would make it langSetting a global varaible.

    The good one is to have JSClass return langSetting or more broadly an object revealing what it, as a module is willing to reveal, this is called the revealing module pattern, here is an excellent book chapter on it by Google developer Addy Osmani

    Here is a good solution in your case:

    var jsClass= function(){
    
    
        var langSetting = "en"; // this is javascript's version of a private variable, it is 'closed' in your jsClass
    
        var getLanguage = function(){
            return langSetting;
        }
    
        var setLanguage = function(){
            navigator.globalization.getPreferredLanguage(function (language){
                langSetting = language.value; //set the local variable
                console.log(langSetting);
            }, function(){
                console.log("there was an error");
            });
        }
        return {
            getLanguage:getLanguage,
            setLanguage:setLanguage
        };
    }
    

    Then in your code you can do

    var obj = new jsClass(); // is exactly the same as var obj = jsClass , when functions return objects it doesn't matter if you use the 'new' keyword
    obj.getLanguage(); //returns en
    

    Another solution would be to use the constructor pattern. Some consider the constructor pattern as a mistake made to implement java developers migrating to javascript although it is widely used in the industry and in a lot of libraries. This would look the following way:

    function JSClass(){ //notice the capital J which though doesn't change execution is a convention indicating a class constructor
        var langSetting;
        this.setLanguage = function(){
            //setter code here
        }
        this.getLanguage = function(){
            //getter code here
        }
    }
    

    and then use the new keyword, it would look like:

    var myClass = new JSClass();
    myClass.getLanguage();//getter result
    

    Also, note I’m noticing that you’re using Apache Cordova API for globalization, please note that makes setLanguage asynchronous, so you might want to have it accept a callback parameter which means you get to ‘hook’ on when it completes. See This stackoverflow question for more details on callbacks and how to use them.

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

Sidebar

Related Questions

here is the problem.. i have a javascript file which is named javascript.js like
I have a javascript file which has a function calling a server (url) to
I have a JavaScript file which has a variable as such: var ads =
I have a url like this : http://www.refskou.dk/safari-%F8.html The file is named like this:
I have an ASP.NET MVC website which has a particular javascript file that needs
In my HTML file, I have a JavaScript function named CheckCaptcha. I am using
I currently have this Javascript in a file named getresults.js: function getItems(str) { if
I have a js file named bars.js. This javascript file contains the code to
I have a JavaScript file which is loaded up at the end of my
I have a javascript file main.js and five html files 1.html,2.html,3.html,4.html,5.html I want to

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.