Guy, The function is returning an object, pretty much like { "foo": "bar" }, but it happens to contain the functions the it wants to expose to the caller.
Gus On Thu, Jun 25, 2015 at 2:44 PM, Guy Duff <guyd...@gmail.com> wrote: > Hi, and thanks for having this forum. I am reading the book entitled > beginning node.js by Basarat Ali Syed > <http://www.amazon.com/s/ref=dp_byline_sr_book_1?ie=UTF8&field-author=Basarat+Ali+Syed&search-alias=books&text=Basarat+Ali+Syed&sort=relevancerank>. > There is a code example that I am having a little trouble understanding: > > function printableMessage() { > var message = 'hello'; > function setMessage(newMessage) { > if(!newMessage) throw new Error('cannot set empty message'); > message = newMessage; > } > > function getMessage() { > return message; > } > > function printMessage() { > console.log(message); > } > > return { > setMessage: setMessage, > getMessage: getMessage, > printMessage: printMessage > }; > } > > // Pattern in use > var awesome1 = printableMessage(); > awesome1.printMessage(); > > var awesome2 = printableMessage(); > awesome2.setMessage('A new message for you.'); > awesome2.printMessage(); > " > > I understand everything except the syntax in the return section. Why is > the label the same as function name in each case in the return section. > Why is a return necessary to execute the function at all? This code was > designed to illustrate the concept of "revealing modules"? I would be very > grateful if anyone can elaborate on how this example reveals modules. Are > each of the functions a "module"? If this is so, am I to assume that the > return function is responsible for revealing them? > > It seems like the functions are methods of the printableMessage function? > Anyway, sorry for asking such a nube question. I really want to understand > node.js, not just hack into to develop. - Thanks very much. > > -- > Job board: http://jobs.nodejs.org/ > New group rules: > https://gist.github.com/othiym23/9886289#file-moderation-policy-md > Old group rules: > 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 unsubscribe from this group and stop receiving emails from it, send an > email to nodejs+unsubscr...@googlegroups.com. > To post to this group, send email to nodejs@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/nodejs/edcca2f5-4742-419a-a17b-42657c6cd97f%40googlegroups.com > <https://groups.google.com/d/msgid/nodejs/edcca2f5-4742-419a-a17b-42657c6cd97f%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- Job board: http://jobs.nodejs.org/ New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md Old group rules: 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 unsubscribe from this group and stop receiving emails from it, send an email to nodejs+unsubscr...@googlegroups.com. To post to this group, send email to nodejs@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/CAJkwh4%2BASRbXe%3Dp8-dHZVSdh5w5m81maouoHZCZELjc0PTW8cw%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.