Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I've been trying to use a Single JavaScript object in multiple files. For that I chose singleton design pattern in JavaScript.

mySingleton.js

var mySingleton = (function () {
var instance;
function init() {
function privateMethod(){
    console.log( "I am private" );
}
var privateVariable = "Im also private";
var privateRandomNumber = Math.random();

return {

  publicProperty: "I am also public",
  publicMethod: function () {
    console.log( "The public can see me!" );
  },

  getRandomNumber: function() {
    return privateRandomNumber;
  }

  };

};

return { 

getInstance: function () {
  if ( !instance ) {
    instance = init();
    console.log("Newly creating an object");
  } 
  return instance;
} 
};
})();
module.exports = mySingleton;

And i'm Accessing the The above object in a separate node js file. As showed in following code

test.js

var singleton = require('./mySingleton');
var obj = singleton.getInstance();
console.log(obj.publicProperty);
console.log('random number value:'+obj.getRandomNumber());

test2.js

var singleton = require('./mySingleton');
var obj = singleton.getInstance();
console.log(obj.publicProperty);
console.log('random number value:'+obj.getRandomNumber());

When i execute the above two files each time a new javascript object is creating. But I want to use the same JavaScript object in multiple files.

Thanks in advance....

So Can anyone please give any suggestion to achieve the above functionality.

share|improve this question
    
You should remove the java tag. Java is a programming language that has nothing to do with Javascript :) –  Vincent Durmont Jan 13 at 7:11
    
are you using any framework, like Express 4.0? –  Datsik Jan 13 at 7:13
    
are you executing test.js and test2.js independently one by one? –  Anurag Peshne Jan 13 at 7:15
    
yes, @Anurag Peshne –  Prabhu Jan 13 at 7:17
    
Are you doing node test.js then running node test2.js? –  Datsik Jan 13 at 7:18

2 Answers 2

The issue is that you are running test.js and test2.js independently. When you execute test.js, it creates a global context where obj binds to global scope of test.js. After the execution is complete, that context is destroyed and a new context is created for test2.js. Hence the instance variable inside getInstance function does not find the instance and creates a new one. To correctly use single pattern you need to use same singleton variable to get instances.

Thus once you have created singleton using

var singleton = require('./mySingleton');

you need to get all the instances using this variable only. Since the singleton property is arising due to closure of execution of anonymous function which returns object to mySingleton

share|improve this answer

You're calling them individually which will not work, every time you call node test.js you're creating a new run time variable:

If you were to include them into a main package and run it, you would see that it works just fine:

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.