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 ?
There are two ways to solve this, a bad and common one and a good one,
The bad one is to remove the
varkey word fromlangSettingwhich would make itlangSettinga global varaible.The good one is to have JSClass return
langSettingor 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 OsmaniHere is a good solution in your case:
Then in your code you can do
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:
and then use the
newkeyword, it would look like: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 parameterwhich means you get to ‘hook’ on when it completes. See This stackoverflow question for more details on callbacks and how to use them.