yeah that's right. You just need to add the line
module.exports = mySingleton
and in the caller file require as follows
var singleton = require('./single').mySingleton
Hope that helps
José
On Nov 19, 2013, at 2:40 AM, Reza Razavipour <[email protected]> wrote:
> Wonderful book, thank you.
>
> So reading the book, if I put the code for the mySingleton class into a file
> on its own, call it single.js.
> In my main.js, I add that with a require statement, such as var singleton =
> require('./.single);
> when I say, singleton.getInstance(), I get a compile error saying singleton
> does not have a getInstance function...
>
> Do I have to add an export to the single.js file I created or what am I
> missing...
>
> The code is as follows:
>
> 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
>
>
>
>
> On Monday, November 18, 2013 3:38:09 PM UTC-8, Reza Razavipour wrote:
> A newbie question...
>
> I have an app that connects and reuses the same connection to a remote
> database and a connection to a remote soap server.
> I want to implement a singleton pattern for each of these. I am used to doing
> that in C++ and Java but want to know what the standard
> implementation for a Singleton pattern is in node.js.
>
> Any recommendations or references.
>
>
>
> --
> --
> 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> 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 [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
--
--
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/groups/opt_out.