javascript - Return in Revealing Module Pattern -
i read addy's book here revealing module patter. however, if execute example code returns undefined. fix add 'return' before each called functions. supposed add return each functions being called if using rmp? right way make work? missing?
var myrevealingmodule = (function () { var privatecounter = 0; function privatefunction() { privatecounter++; <--need add return } function publicfunction() { publicincrement(); <-- need add return } function publicincrement() { privatefunction(); <--need add return } function publicgetcount(){ return privatecounter; } // reveal public pointers // private functions , properties return { start: publicfunction, increment: publicincrement, count: publicgetcount }; })(); myrevealingmodule.start(); <-return undefined
http://addyosmani.com/resources/essentialjsdesignpatterns/book/#revealingmodulepatternjavascript
the issue nas nothing rmp rather functions , return values.
why expect method doesn't return return other undefined?
take closer here. start
in fact calls publicfunction
body of latter doesn't return anything.
yet call , expect value.
the answer question then: yes, if want value function, have return it.
in particlar example have method count
return current value. 2 other methods used control counter.
Comments
Post a Comment