I wrote a simple JS script to create an object that permit the inizialization of only one instance. If the object is already inizialized it return is without recreate it again.
This is my: myobj.js
var Singleton = {
initialized : null,
init: function(){
console.log("new obj...");
this.initialized = {
'status' : 'initialized'
}
},
getInstance : function(){
if (!this.initialized) this.init();
return this.initialized;
}
}
Then I have create a test.html page to test this script:
<script src="myobj.js"></script>
<script>
var uno = Singleton.getInstance();
var due = Singleton.getInstance();
(uno===due) ? console.log("equals") : console.log("not equals");
</script>
All works good, only one object is created.
My question is: can I share this object "Singleton" between more HTML pages?
without recreate it in different pages?
If I have (for example) 100 tabs opened on the browser, I would like to use the same object without having the same 100 objects.
Thank you!