Monthly Archives : August 2010

Create singleton in javascript

Post on August 31st,2010 by Posted in programming languages| Tagged in | No Comment


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…