--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
var mySingleton = (function() {
var instance;
function init() {
var privateRandomNumber = Math.random();
return {
publicMethod : function() {
console.log("The public can see me!");
},
publicProperty : "I am also public",
getRandomNumber : function() {
return privateRandomNumber;
},
getInstance : function() {
if (!instance) {
instance = init();
}
return instance;
}
};
};
return {
getInstance : function() {
if (!instance) {
instance = init();
}
return instance;
}
};
})();
Thanks for the help.
Reza
function getAnObject(a) { var anObject; var b = a + 1; return (getAnObject = function() { if(! anObject ) { anObject = {name: b}; } return anObject; })(); }
var anObject = new function(a) { var b = a + 2; this.name = b; };
From https://noisebridge.net/wiki/JavaScript/Notes/Singleton
Class is Friday, 7pm, at Noisebridge.
Oh yeah, and in case you were wondering, it's fre.
> For example, if I have a module for emailing with a send() method on it, I don't have it export a constructor; I have it export an object. That object might maintain some state or it might not. When the module is subsequently require()'d, it will have any state that it has accumulated since.
For.most of the time, that's unnecessary - multiple require() calls for the same module return same, cached module, so you can store state just by using local variables in that module.
> For example, if I have a module for emailing with a send() method on it, I don't have it export a constructor; I have it export an object. That object might maintain some state or it might not. When the module is subsequently require()'d, it will have any state that it has accumulated since.
For.most of the time, that's unnecessary - multiple require() calls for the same module return same, cached module, so you can store state just by using local variables in that module.
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/GmUto9AN47U/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.
Aah, nevermind then, I misunerstood ;)
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
var SoapClient = function() {
var soap = require('soap');
this.init = function (){
var url = "http://172.31.19.39/MgmtServer.wsdl";
var endPoint = "https://172.31.19.39:9088";
var options = {};
options.endpoint = endPoint;
soap.createClient(url, options, function(err, result) {
if (err) {
console.log('soap client create failed ' + err);
return;
}
console.log('init is called ready');
SoapClient.instance = result;
SoapClient.instance.setSecurity(new soap.BasicAuthSecurity(
'admin-priv', 'password'));
});
};
};
SoapClient.getInstance = function () {
if (SoapClient.instance) {
return SoapClient.instance;
}
new SoapClient().init();
return SoapClient.instance;
};
SoapClient.instance = null;
module.exports = SoapClient;
Thoughts
So how do you achieve the same effect in javascript? In the browser, you have globals.
If you want just one instance of a thing, create it, and set it to a global variable. You can use that global variable everywhere. (If you're thinking "but global variables are bad!", I mostly agree. This is one of the reasons that the singleton itself is actually considered an anti-pattern by many.
--
of course you could just as easy have done...