Below javascript code will create the helloWorld variable as a singleton
<script type=”text/javascript”>
var helloWorld = new function() {
this.hello = {
sayHi : function() {
alert(“Hello World”);
}
};
}();
</script>
In order to access the alert method inside that variable, all you need is to use the dot notation…
var sayHitotheworld = new helloWorld.hello.sayHi();
now click on the below button to see the outcome…
